* gnus-group.el (gnus-group-make-doc-group): Work for non-ASCII group names.
[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 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 the article under point.
312 Which article this is is controlled by the `gnus-auto-select-subject'
313 variable.
314
315 If you want to prevent automatic selection of articles in some
316 newsgroups, set the variable to nil in `gnus-select-group-hook'."
317   :group 'gnus-group-select
318   :type '(choice (const :tag "none" nil)
319                  (sexp :menu-tag "first" t)))
320
321 (defcustom gnus-auto-select-subject 'unread
322   "*Says what subject to place under point when entering a group.
323
324 This variable can either be the symbols `first' (place point on the
325 first subject), `unread' (place point on the subject line of the first
326 unread article), `best' (place point on the subject line of the
327 higest-scored article), `unseen' (place point on the subject line of
328 the first unseen article), `unseen-or-unread' (place point on the subject
329 line of the first unseen article or, if all article have been seen, on the
330 subject line of the first unread article), or a function to be called to
331 place point on some subject line."
332   :version "22.1"
333   :group 'gnus-group-select
334   :type '(choice (const best)
335                  (const unread)
336                  (const first)
337                  (const unseen)
338                  (const unseen-or-unread)))
339
340 (defcustom gnus-auto-select-next t
341   "*If non-nil, offer to go to the next group from the end of the previous.
342 If the value is t and the next newsgroup is empty, Gnus will exit
343 summary mode and go back to group mode.  If the value is neither nil
344 nor t, Gnus will select the following unread newsgroup.  In
345 particular, if the value is the symbol `quietly', the next unread
346 newsgroup will be selected without any confirmation, and if it is
347 `almost-quietly', the next group will be selected without any
348 confirmation if you are located on the last article in the group.
349 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
350 will go to the next group without confirmation."
351   :group 'gnus-summary-maneuvering
352   :type '(choice (const :tag "off" nil)
353                  (const quietly)
354                  (const almost-quietly)
355                  (const slightly-quietly)
356                  (sexp :menu-tag "on" t)))
357
358 (defcustom gnus-auto-select-same nil
359   "*If non-nil, select the next article with the same subject.
360 If there are no more articles with the same subject, go to
361 the first unread article."
362   :group 'gnus-summary-maneuvering
363   :type 'boolean)
364
365 (defcustom gnus-auto-goto-ignores 'unfetched
366   "*Says how to handle unfetched articles when maneuvering.
367
368 This variable can either be the symbols nil (maneuver to any
369 article), `undownloaded' (maneuvering while unplugged ignores articles
370 that have not been fetched), `always-undownloaded' (maneuvering always
371 ignores articles that have not been fetched), `unfetched' (maneuvering
372 ignores articles whose headers have not been fetched).
373
374 NOTE: The list of unfetched articles will always be nil when plugged
375 and, when unplugged, a subset of the undownloaded article list."
376   :version "22.1"
377   :group 'gnus-summary-maneuvering
378   :type '(choice (const :tag "None" nil)
379                  (const :tag "Undownloaded when unplugged" undownloaded)
380                  (const :tag "Undownloaded" always-undownloaded)
381                  (const :tag "Unfetched" unfetched)))
382
383 (defcustom gnus-summary-check-current nil
384   "*If non-nil, consider the current article when moving.
385 The \"unread\" movement commands will stay on the same line if the
386 current article is unread."
387   :group 'gnus-summary-maneuvering
388   :type 'boolean)
389
390 (defcustom gnus-auto-center-summary 2
391   "*If non-nil, always center the current summary buffer.
392 In particular, if `vertical' do only vertical recentering.  If non-nil
393 and non-`vertical', do both horizontal and vertical recentering."
394   :group 'gnus-summary-maneuvering
395   :type '(choice (const :tag "none" nil)
396                  (const vertical)
397                  (integer :tag "height")
398                  (sexp :menu-tag "both" t)))
399
400 (defvar gnus-auto-center-group t
401   "*If non-nil, always center the group buffer.")
402
403 (defcustom gnus-show-all-headers nil
404   "*If non-nil, don't hide any headers."
405   :group 'gnus-article-hiding
406   :group 'gnus-article-headers
407   :type 'boolean)
408
409 (defcustom gnus-summary-ignore-duplicates nil
410   "*If non-nil, ignore articles with identical Message-ID headers."
411   :group 'gnus-summary
412   :type 'boolean)
413
414 (defcustom gnus-single-article-buffer t
415   "*If non-nil, display all articles in the same buffer.
416 If nil, each group will get its own article buffer."
417   :group 'gnus-article-various
418   :type 'boolean)
419
420 (defcustom gnus-break-pages t
421   "*If non-nil, do page breaking on articles.
422 The page delimiter is specified by the `gnus-page-delimiter'
423 variable."
424   :group 'gnus-article-various
425   :type 'boolean)
426
427 (defcustom gnus-move-split-methods nil
428   "*Variable used to suggest where articles are to be moved to.
429 It uses the same syntax as the `gnus-split-methods' variable.
430 However, whereas `gnus-split-methods' specifies file names as targets,
431 this variable specifies group names."
432   :group 'gnus-summary-mail
433   :type '(repeat (choice (list :value (fun) function)
434                          (cons :value ("" "") regexp (repeat string))
435                          (sexp :value nil))))
436
437 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
438   "Function used to compute default prefix for article move/copy/etc prompts.
439 The function should take one argument, a group name, and return a
440 string with the suggested prefix."
441   :group 'gnus-summary-mail
442   :type 'function)
443
444 ;; FIXME: Although the custom type is `character' for the following variables,
445 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
446
447 (defcustom gnus-unread-mark ?           ;Whitespace
448   "*Mark used for unread articles."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-ticked-mark ?!
453   "*Mark used for ticked articles."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-dormant-mark ??
458   "*Mark used for dormant articles."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-del-mark ?r
463   "*Mark used for del'd articles."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-read-mark ?R
468   "*Mark used for read articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-expirable-mark ?E
473   "*Mark used for expirable articles."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-killed-mark ?K
478   "*Mark used for killed articles."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-spam-mark ?$
483   "*Mark used for spam articles."
484   :version "22.1"
485   :group 'gnus-summary-marks
486   :type 'character)
487
488 (defcustom gnus-souped-mark ?F
489   "*Mark used for souped articles."
490   :group 'gnus-summary-marks
491   :type 'character)
492
493 (defcustom gnus-kill-file-mark ?X
494   "*Mark used for articles killed by kill files."
495   :group 'gnus-summary-marks
496   :type 'character)
497
498 (defcustom gnus-low-score-mark ?Y
499   "*Mark used for articles with a low score."
500   :group 'gnus-summary-marks
501   :type 'character)
502
503 (defcustom gnus-catchup-mark ?C
504   "*Mark used for articles that are caught up."
505   :group 'gnus-summary-marks
506   :type 'character)
507
508 (defcustom gnus-replied-mark ?A
509   "*Mark used for articles that have been replied to."
510   :group 'gnus-summary-marks
511   :type 'character)
512
513 (defcustom gnus-forwarded-mark ?F
514   "*Mark used for articles that have been forwarded."
515   :version "22.1"
516   :group 'gnus-summary-marks
517   :type 'character)
518
519 (defcustom gnus-recent-mark ?N
520   "*Mark used for articles that are recent."
521   :version "22.1"
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-cached-mark ?*
526   "*Mark used for articles that are in the cache."
527   :group 'gnus-summary-marks
528   :type 'character)
529
530 (defcustom gnus-saved-mark ?S
531   "*Mark used for articles that have been saved."
532   :group 'gnus-summary-marks
533   :type 'character)
534
535 (defcustom gnus-unseen-mark ?.
536   "*Mark used for articles that haven't been seen."
537   :version "22.1"
538   :group 'gnus-summary-marks
539   :type 'character)
540
541 (defcustom gnus-no-mark ?               ;Whitespace
542   "*Mark used for articles that have no other secondary mark."
543   :version "22.1"
544   :group 'gnus-summary-marks
545   :type 'character)
546
547 (defcustom gnus-ancient-mark ?O
548   "*Mark used for ancient articles."
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-sparse-mark ?Q
553   "*Mark used for sparsely reffed articles."
554   :group 'gnus-summary-marks
555   :type 'character)
556
557 (defcustom gnus-canceled-mark ?G
558   "*Mark used for canceled articles."
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-duplicate-mark ?M
563   "*Mark used for duplicate articles."
564   :group 'gnus-summary-marks
565   :type 'character)
566
567 (defcustom gnus-undownloaded-mark ?-
568   "*Mark used for articles that weren't downloaded."
569   :version "22.1"
570   :group 'gnus-summary-marks
571   :type 'character)
572
573 (defcustom gnus-downloaded-mark ?+
574   "*Mark used for articles that were downloaded."
575   :group 'gnus-summary-marks
576   :type 'character)
577
578 (defcustom gnus-downloadable-mark ?%
579   "*Mark used for articles that are to be downloaded."
580   :group 'gnus-summary-marks
581   :type 'character)
582
583 (defcustom gnus-unsendable-mark ?=
584   "*Mark used for articles that won't be sent."
585   :group 'gnus-summary-marks
586   :type 'character)
587
588 (defcustom gnus-score-over-mark ?+
589   "*Score mark used for articles with high scores."
590   :group 'gnus-summary-marks
591   :type 'character)
592
593 (defcustom gnus-score-below-mark ?-
594   "*Score mark used for articles with low scores."
595   :group 'gnus-summary-marks
596   :type 'character)
597
598 (defcustom gnus-empty-thread-mark ?     ;Whitespace
599   "*There is no thread under the article."
600   :group 'gnus-summary-marks
601   :type 'character)
602
603 (defcustom gnus-not-empty-thread-mark ?=
604   "*There is a thread under the article."
605   :group 'gnus-summary-marks
606   :type 'character)
607
608 (defcustom gnus-view-pseudo-asynchronously nil
609   "*If non-nil, Gnus will view pseudo-articles asynchronously."
610   :group 'gnus-extract-view
611   :type 'boolean)
612
613 (defcustom gnus-auto-expirable-marks
614   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
615         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
616         gnus-souped-mark gnus-duplicate-mark)
617   "*The list of marks converted into expiration if a group is auto-expirable."
618   :version "21.1"
619   :group 'gnus-summary
620   :type '(repeat character))
621
622 (defcustom gnus-inhibit-user-auto-expire t
623   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
624   :version "21.1"
625   :group 'gnus-summary
626   :type 'boolean)
627
628 (defcustom gnus-view-pseudos nil
629   "*If `automatic', pseudo-articles will be viewed automatically.
630 If `not-confirm', pseudos will be viewed automatically, and the user
631 will not be asked to confirm the command."
632   :group 'gnus-extract-view
633   :type '(choice (const :tag "off" nil)
634                  (const automatic)
635                  (const not-confirm)))
636
637 (defcustom gnus-view-pseudos-separately t
638   "*If non-nil, one pseudo-article will be created for each file to be viewed.
639 If nil, all files that use the same viewing command will be given as a
640 list of parameters to that command."
641   :group 'gnus-extract-view
642   :type 'boolean)
643
644 (defcustom gnus-insert-pseudo-articles t
645   "*If non-nil, insert pseudo-articles when decoding articles."
646   :group 'gnus-extract-view
647   :type 'boolean)
648
649 (defcustom gnus-summary-dummy-line-format
650   "   %(:                             :%) %S\n"
651   "*The format specification for the dummy roots in the summary buffer.
652 It works along the same lines as a normal formatting string,
653 with some simple extensions.
654
655 %S  The subject
656
657 General format specifiers can also be used.
658 See `(gnus)Formatting Variables'."
659   :link '(custom-manual "(gnus)Formatting Variables")
660   :group 'gnus-threading
661   :type 'string)
662
663 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
664   "*The format specification for the summary mode line.
665 It works along the same lines as a normal formatting string,
666 with some simple extensions:
667
668 %G  Group name
669 %p  Unprefixed group name
670 %A  Current article number
671 %z  Current article score
672 %V  Gnus version
673 %U  Number of unread articles in the group
674 %e  Number of unselected articles in the group
675 %Z  A string with unread/unselected article counts
676 %g  Shortish group name
677 %S  Subject of the current article
678 %u  User-defined spec
679 %s  Current score file name
680 %d  Number of dormant articles
681 %r  Number of articles that have been marked as read in this session
682 %E  Number of articles expunged by the score files"
683   :group 'gnus-summary-format
684   :type 'string)
685
686 (defcustom gnus-list-identifiers nil
687   "Regexp that matches list identifiers to be removed from subject.
688 This can also be a list of regexps."
689   :version "21.1"
690   :group 'gnus-summary-format
691   :group 'gnus-article-hiding
692   :type '(choice (const :tag "none" nil)
693                  (regexp :value ".*")
694                  (repeat :value (".*") regexp)))
695
696 (defcustom gnus-summary-mark-below 0
697   "*Mark all articles with a score below this variable as read.
698 This variable is local to each summary buffer and usually set by the
699 score file."
700   :group 'gnus-score-default
701   :type 'integer)
702
703 (defun gnus-widget-reversible-match (widget value)
704   "Ignoring WIDGET, convert VALUE to internal form.
705 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
706   ;; (debug value)
707   (or (symbolp value)
708       (and (listp value)
709            (eq (length value) 2)
710            (eq (nth 0 value) 'not)
711            (symbolp (nth 1 value)))))
712
713 (defun gnus-widget-reversible-to-internal (widget value)
714   "Ignoring WIDGET, convert VALUE to internal form.
715 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
716 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
717   ;; (debug value)
718   (if (atom value)
719       (list value nil)
720     (list (nth 1 value) t)))
721
722 (defun gnus-widget-reversible-to-external (widget value)
723   "Ignoring WIDGET, convert VALUE to external form.
724 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
725 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
726   ;; (debug value)
727   (if (nth 1 value)
728       (list 'not (nth 0 value))
729     (nth 0 value)))
730
731 (define-widget 'gnus-widget-reversible 'group
732   "A `group' that convert values."
733   :match 'gnus-widget-reversible-match
734   :value-to-internal 'gnus-widget-reversible-to-internal
735   :value-to-external 'gnus-widget-reversible-to-external)
736
737 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
738   "*List of functions used for sorting articles in the summary buffer.
739
740 Each function takes two articles and returns non-nil if the first
741 article should be sorted before the other.  If you use more than one
742 function, the primary sort function should be the last.  You should
743 probably always include `gnus-article-sort-by-number' in the list of
744 sorting functions -- preferably first.  Also note that sorting by date
745 is often much slower than sorting by number, and the sorting order is
746 very similar.  (Sorting by date means sorting by the time the message
747 was sent, sorting by number means sorting by arrival time.)
748
749 Each item can also be a list `(not F)' where F is a function;
750 this reverses the sort order.
751
752 Ready-made functions include `gnus-article-sort-by-number',
753 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
754 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
755 and `gnus-article-sort-by-score'.
756
757 When threading is turned on, the variable `gnus-thread-sort-functions'
758 controls how articles are sorted."
759   :group 'gnus-summary-sort
760   :type '(repeat (gnus-widget-reversible
761                   (choice (function-item gnus-article-sort-by-number)
762                           (function-item gnus-article-sort-by-author)
763                           (function-item gnus-article-sort-by-subject)
764                           (function-item gnus-article-sort-by-date)
765                           (function-item gnus-article-sort-by-score)
766                           (function-item gnus-article-sort-by-random)
767                           (function :tag "other"))
768                   (boolean :tag "Reverse order"))))
769
770
771 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
772   "*List of functions used for sorting threads in the summary buffer.
773 By default, threads are sorted by article number.
774
775 Each function takes two threads and returns non-nil if the first
776 thread should be sorted before the other.  If you use more than one
777 function, the primary sort function should be the last.  You should
778 probably always include `gnus-thread-sort-by-number' in the list of
779 sorting functions -- preferably first.  Also note that sorting by date
780 is often much slower than sorting by number, and the sorting order is
781 very similar.  (Sorting by date means sorting by the time the message
782 was sent, sorting by number means sorting by arrival time.)
783
784 Each list item can also be a list `(not F)' where F is a
785 function; this specifies reversed sort order.
786
787 Ready-made functions include `gnus-thread-sort-by-number',
788 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
789 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
790 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
791 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
792 and `gnus-thread-sort-by-total-score' (see
793 `gnus-thread-score-function').
794
795 When threading is turned off, the variable
796 `gnus-article-sort-functions' controls how articles are sorted."
797   :group 'gnus-summary-sort
798   :type '(repeat
799           (gnus-widget-reversible
800            (choice (function-item gnus-thread-sort-by-number)
801                    (function-item gnus-thread-sort-by-author)
802                    (function-item gnus-thread-sort-by-recipient)
803                    (function-item gnus-thread-sort-by-subject)
804                    (function-item gnus-thread-sort-by-date)
805                    (function-item gnus-thread-sort-by-score)
806                    (function-item gnus-thread-sort-by-most-recent-number)
807                    (function-item gnus-thread-sort-by-most-recent-date)
808                    (function-item gnus-thread-sort-by-random)
809                    (function-item gnus-thread-sort-by-total-score)
810                    (function :tag "other"))
811            (boolean :tag "Reverse order"))))
812
813 (defcustom gnus-thread-score-function '+
814   "*Function used for calculating the total score of a thread.
815
816 The function is called with the scores of the article and each
817 subthread and should then return the score of the thread.
818
819 Some functions you can use are `+', `max', or `min'."
820   :group 'gnus-summary-sort
821   :type 'function)
822
823 (defcustom gnus-summary-expunge-below nil
824   "All articles that have a score less than this variable will be expunged.
825 This variable is local to the summary buffers."
826   :group 'gnus-score-default
827   :type '(choice (const :tag "off" nil)
828                  integer))
829
830 (defcustom gnus-thread-expunge-below nil
831   "All threads that have a total score less than this variable will be expunged.
832 See `gnus-thread-score-function' for en explanation of what a
833 \"thread score\" is.
834
835 This variable is local to the summary buffers."
836   :group 'gnus-threading
837   :group 'gnus-score-default
838   :type '(choice (const :tag "off" nil)
839                  integer))
840
841 (defcustom gnus-summary-mode-hook nil
842   "*A hook for Gnus summary mode.
843 This hook is run before any variables are set in the summary buffer."
844   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
845   :group 'gnus-summary-various
846   :type 'hook)
847
848 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
849 (when (featurep 'xemacs)
850   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
851   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
852   (add-hook 'gnus-summary-mode-hook
853             'gnus-xmas-switch-horizontal-scrollbar-off))
854
855 (defcustom gnus-summary-menu-hook nil
856   "*Hook run after the creation of the summary mode menu."
857   :group 'gnus-summary-visual
858   :type 'hook)
859
860 (defcustom gnus-summary-exit-hook nil
861   "*A hook called on exit from the summary buffer.
862 It will be called with point in the group buffer."
863   :group 'gnus-summary-exit
864   :type 'hook)
865
866 (defcustom gnus-summary-prepare-hook nil
867   "*A hook called after the summary buffer has been generated.
868 If you want to modify the summary buffer, you can use this hook."
869   :group 'gnus-summary-various
870   :type 'hook)
871
872 (defcustom gnus-summary-prepared-hook nil
873   "*A hook called as the last thing after the summary buffer has been generated."
874   :group 'gnus-summary-various
875   :type 'hook)
876
877 (defcustom gnus-summary-generate-hook nil
878   "*A hook run just before generating the summary buffer.
879 This hook is commonly used to customize threading variables and the
880 like."
881   :group 'gnus-summary-various
882   :type 'hook)
883
884 (defcustom gnus-select-group-hook nil
885   "*A hook called when a newsgroup is selected.
886
887 If you'd like to simplify subjects like the
888 `gnus-summary-next-same-subject' command does, you can use the
889 following hook:
890
891  (add-hook gnus-select-group-hook
892            (lambda ()
893              (mapcar (lambda (header)
894                        (mail-header-set-subject
895                         header
896                         (gnus-simplify-subject
897                          (mail-header-subject header) 're-only)))
898                      gnus-newsgroup-headers)))"
899   :group 'gnus-group-select
900   :type 'hook)
901
902 (defcustom gnus-select-article-hook nil
903   "*A hook called when an article is selected."
904   :group 'gnus-summary-choose
905   :options '(gnus-agent-fetch-selected-article)
906   :type 'hook)
907
908 (defcustom gnus-visual-mark-article-hook
909   (list 'gnus-highlight-selected-summary)
910   "*Hook run after selecting an article in the summary buffer.
911 It is meant to be used for highlighting the article in some way.  It
912 is not run if `gnus-visual' is nil."
913   :group 'gnus-summary-visual
914   :type 'hook)
915
916 (defcustom gnus-parse-headers-hook nil
917   "*A hook called before parsing the headers."
918   :group 'gnus-various
919   :type 'hook)
920
921 (defcustom gnus-exit-group-hook nil
922   "*A hook called when exiting summary mode.
923 This hook is not called from the non-updating exit commands like `Q'."
924   :group 'gnus-various
925   :type 'hook)
926
927 (defcustom gnus-summary-update-hook
928   (list 'gnus-summary-highlight-line)
929   "*A hook called when a summary line is changed.
930 The hook will not be called if `gnus-visual' is nil.
931
932 The default function `gnus-summary-highlight-line' will
933 highlight the line according to the `gnus-summary-highlight'
934 variable."
935   :group 'gnus-summary-visual
936   :type 'hook)
937
938 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
939   "*A hook called when an article is selected for the first time.
940 The hook is intended to mark an article as read (or unread)
941 automatically when it is selected."
942   :group 'gnus-summary-choose
943   :type 'hook)
944
945 (defcustom gnus-group-no-more-groups-hook nil
946   "*A hook run when returning to group mode having no more (unread) groups."
947   :group 'gnus-group-select
948   :type 'hook)
949
950 (defcustom gnus-ps-print-hook nil
951   "*A hook run before ps-printing something from Gnus."
952   :group 'gnus-summary
953   :type 'hook)
954
955 (defcustom gnus-summary-article-move-hook nil
956   "*A hook called after an article is moved, copied, respooled, or crossposted."
957   :version "22.1"
958   :group 'gnus-summary
959   :type 'hook)
960
961 (defcustom gnus-summary-article-delete-hook nil
962   "*A hook called after an article is deleted."
963   :version "22.1"
964   :group 'gnus-summary
965   :type 'hook)
966
967 (defcustom gnus-summary-article-expire-hook nil
968   "*A hook called after an article is expired."
969   :version "22.1"
970   :group 'gnus-summary
971   :type 'hook)
972
973 (defcustom gnus-summary-display-arrow
974   (and (fboundp 'display-graphic-p)
975        (display-graphic-p))
976   "*If non-nil, display an arrow highlighting the current article."
977   :version "22.1"
978   :group 'gnus-summary
979   :type 'boolean)
980
981 (defcustom gnus-summary-selected-face 'gnus-summary-selected
982   "Face used for highlighting the current article in the summary buffer."
983   :group 'gnus-summary-visual
984   :type 'face)
985
986 (defvar gnus-tmp-downloaded nil)
987
988 (defcustom gnus-summary-highlight
989   '(((eq mark gnus-canceled-mark)
990      . gnus-summary-cancelled)
991     ((and uncached (> score default-high))
992      . gnus-summary-high-undownloaded)
993     ((and uncached (< score default-low))
994      . gnus-summary-low-undownloaded)
995     (uncached
996      . gnus-summary-normal-undownloaded)
997     ((and (> score default-high)
998           (or (eq mark gnus-dormant-mark)
999               (eq mark gnus-ticked-mark)))
1000      . gnus-summary-high-ticked)
1001     ((and (< score default-low)
1002           (or (eq mark gnus-dormant-mark)
1003               (eq mark gnus-ticked-mark)))
1004      . gnus-summary-low-ticked)
1005     ((or (eq mark gnus-dormant-mark)
1006          (eq mark gnus-ticked-mark))
1007      . gnus-summary-normal-ticked)
1008     ((and (> score default-high) (eq mark gnus-ancient-mark))
1009      . gnus-summary-high-ancient)
1010     ((and (< score default-low) (eq mark gnus-ancient-mark))
1011      . gnus-summary-low-ancient)
1012     ((eq mark gnus-ancient-mark)
1013      . gnus-summary-normal-ancient)
1014     ((and (> score default-high) (eq mark gnus-unread-mark))
1015      . gnus-summary-high-unread)
1016     ((and (< score default-low) (eq mark gnus-unread-mark))
1017      . gnus-summary-low-unread)
1018     ((eq mark gnus-unread-mark)
1019      . gnus-summary-normal-unread)
1020     ((> score default-high)
1021      . gnus-summary-high-read)
1022     ((< score default-low)
1023      . gnus-summary-low-read)
1024     (t
1025      . gnus-summary-normal-read))
1026   "*Controls the highlighting of summary buffer lines.
1027
1028 A list of (FORM . FACE) pairs.  When deciding how a a particular
1029 summary line should be displayed, each form is evaluated.  The content
1030 of the face field after the first true form is used.  You can change
1031 how those summary lines are displayed, by editing the face field.
1032
1033 You can use the following variables in the FORM field.
1034
1035 score:        The article's score
1036 default:      The default article score.
1037 default-high: The default score for high scored articles.
1038 default-low:  The default score for low scored articles.
1039 below:        The score below which articles are automatically marked as read.
1040 mark:         The article's mark.
1041 uncached:     Non-nil if the article is uncached."
1042   :group 'gnus-summary-visual
1043   :type '(repeat (cons (sexp :tag "Form" nil)
1044                        face)))
1045
1046 (defcustom gnus-alter-header-function nil
1047   "Function called to allow alteration of article header structures.
1048 The function is called with one parameter, the article header vector,
1049 which it may alter in any way."
1050   :type '(choice (const :tag "None" nil)
1051                  function)
1052   :group 'gnus-summary)
1053
1054 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1055   "Function used to decode a string with encoded words.")
1056
1057 (defvar gnus-decode-encoded-address-function
1058   'mail-decode-encoded-address-string
1059   "Function used to decode addresses with encoded words.")
1060
1061 (defcustom gnus-extra-headers '(To Newsgroups)
1062   "*Extra headers to parse."
1063   :version "21.1"
1064   :group 'gnus-summary
1065   :type '(repeat symbol))
1066
1067 (defcustom gnus-ignored-from-addresses
1068   (and user-mail-address
1069        (not (string= user-mail-address ""))
1070        (regexp-quote user-mail-address))
1071   "*Regexp of From headers that may be suppressed in favor of To headers."
1072   :version "21.1"
1073   :group 'gnus-summary
1074   :type 'regexp)
1075
1076 (defcustom gnus-summary-to-prefix "-> "
1077   "*String prefixed to the To field in the summary line when
1078 using `gnus-ignored-from-addresses'."
1079   :version "22.1"
1080   :group 'gnus-summary
1081   :type 'string)
1082
1083 (defcustom gnus-summary-newsgroup-prefix "=> "
1084   "*String prefixed to the Newsgroup field in the summary
1085 line when using `gnus-ignored-from-addresses'."
1086   :version "22.1"
1087   :group 'gnus-summary
1088   :type 'string)
1089
1090 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1091   "List of charsets that should be ignored.
1092 When these charsets are used in the \"charset\" parameter, the
1093 default charset will be used instead."
1094   :version "21.1"
1095   :type '(repeat symbol)
1096   :group 'gnus-charset)
1097
1098 (gnus-define-group-parameter
1099  ignored-charsets
1100  :type list
1101  :function-document
1102  "Return the ignored charsets of GROUP."
1103  :variable gnus-group-ignored-charsets-alist
1104  :variable-default
1105  '(("alt\\.chinese\\.text" iso-8859-1))
1106  :variable-document
1107  "Alist of regexps (to match group names) and charsets that should be ignored.
1108 When these charsets are used in the \"charset\" parameter, the
1109 default charset will be used instead."
1110  :variable-group gnus-charset
1111  :variable-type '(repeat (cons (regexp :tag "Group")
1112                                (repeat symbol)))
1113  :parameter-type '(choice :tag "Ignored charsets"
1114                           :value nil
1115                           (repeat (symbol)))
1116  :parameter-document       "\
1117 List of charsets that should be ignored.
1118
1119 When these charsets are used in the \"charset\" parameter, the
1120 default charset will be used instead.")
1121
1122 (defcustom gnus-group-highlight-words-alist nil
1123   "Alist of group regexps and highlight regexps.
1124 This variable uses the same syntax as `gnus-emphasis-alist'."
1125   :version "21.1"
1126   :type '(repeat (cons (regexp :tag "Group")
1127                        (repeat (list (regexp :tag "Highlight regexp")
1128                                      (number :tag "Group for entire word" 0)
1129                                      (number :tag "Group for displayed part" 0)
1130                                      (symbol :tag "Face"
1131                                              gnus-emphasis-highlight-words)))))
1132   :group 'gnus-summary-visual)
1133
1134 (defcustom gnus-summary-show-article-charset-alist
1135   nil
1136   "Alist of number and charset.
1137 The article will be shown with the charset corresponding to the
1138 numbered argument.
1139 For example: ((1 . cn-gb-2312) (2 . big5))."
1140   :version "21.1"
1141   :type '(repeat (cons (number :tag "Argument" 1)
1142                        (symbol :tag "Charset")))
1143   :group 'gnus-charset)
1144
1145 (defcustom gnus-preserve-marks t
1146   "Whether marks are preserved when moving, copying and respooling messages."
1147   :version "21.1"
1148   :type 'boolean
1149   :group 'gnus-summary-marks)
1150
1151 (defcustom gnus-alter-articles-to-read-function nil
1152   "Function to be called to alter the list of articles to be selected."
1153   :type '(choice (const nil) function)
1154   :group 'gnus-summary)
1155
1156 (defcustom gnus-orphan-score nil
1157   "*All orphans get this score added.  Set in the score file."
1158   :group 'gnus-score-default
1159   :type '(choice (const nil)
1160                  integer))
1161
1162 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1163   "*A regexp to match MIME parts when saving multiple parts of a
1164 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1165 This regexp will be used by default when prompting the user for which
1166 type of files to save."
1167   :group 'gnus-summary
1168   :type 'regexp)
1169
1170 (defcustom gnus-read-all-available-headers nil
1171   "Whether Gnus should parse all headers made available to it.
1172 This is mostly relevant for slow back ends where the user may
1173 wish to widen the summary buffer to include all headers
1174 that were fetched.  Say, for nnultimate groups."
1175   :version "22.1"
1176   :group 'gnus-summary
1177   :type '(choice boolean regexp))
1178
1179 (defcustom gnus-summary-muttprint-program "muttprint"
1180   "Command (and optional arguments) used to run Muttprint."
1181   :version "22.1"
1182   :group 'gnus-summary
1183   :type 'string)
1184
1185 (defcustom gnus-article-loose-mime t
1186   "If non-nil, don't require MIME-Version header.
1187 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1188 supply the MIME-Version header or deliberately strip it from the mail.
1189 If non-nil (the default), Gnus will treat some articles as MIME
1190 even if the MIME-Version header is missing."
1191   :version "22.1"
1192   :type 'boolean
1193   :group 'gnus-article-mime)
1194
1195 (defcustom gnus-article-emulate-mime t
1196   "If non-nil, use MIME emulation for uuencode and the like.
1197 This means that Gnus will search message bodies for text that look
1198 like uuencoded bits, yEncoded bits, and so on, and present that using
1199 the normal Gnus MIME machinery."
1200   :version "22.1"
1201   :type 'boolean
1202   :group 'gnus-article-mime)
1203
1204 ;;; Internal variables
1205
1206 (defvar gnus-summary-display-cache nil)
1207 (defvar gnus-article-mime-handles nil)
1208 (defvar gnus-article-decoded-p nil)
1209 (defvar gnus-article-charset nil)
1210 (defvar gnus-article-ignored-charsets nil)
1211 (defvar gnus-scores-exclude-files nil)
1212 (defvar gnus-page-broken nil)
1213
1214 (defvar gnus-original-article nil)
1215 (defvar gnus-article-internal-prepare-hook nil)
1216 (defvar gnus-newsgroup-process-stack nil)
1217
1218 (defvar gnus-thread-indent-array nil)
1219 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1220 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1221   "Function called to sort the articles within a thread after it has been gathered together.")
1222
1223 (defvar gnus-summary-save-parts-type-history nil)
1224 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1225
1226 ;; Avoid highlighting in kill files.
1227 (defvar gnus-summary-inhibit-highlight nil)
1228 (defvar gnus-newsgroup-selected-overlay nil)
1229 (defvar gnus-inhibit-limiting nil)
1230 (defvar gnus-newsgroup-adaptive-score-file nil)
1231 (defvar gnus-current-score-file nil)
1232 (defvar gnus-current-move-group nil)
1233 (defvar gnus-current-copy-group nil)
1234 (defvar gnus-current-crosspost-group nil)
1235 (defvar gnus-newsgroup-display nil)
1236
1237 (defvar gnus-newsgroup-dependencies nil)
1238 (defvar gnus-newsgroup-adaptive nil)
1239 (defvar gnus-summary-display-article-function nil)
1240 (defvar gnus-summary-highlight-line-function nil
1241   "Function called after highlighting a summary line.")
1242
1243 (defvar gnus-summary-line-format-alist
1244   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1245     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1246     (?s gnus-tmp-subject-or-nil ?s)
1247     (?n gnus-tmp-name ?s)
1248     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1249         ?s)
1250     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1251             gnus-tmp-from) ?s)
1252     (?F gnus-tmp-from ?s)
1253     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1254     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1255     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1256     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1257     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1258     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1259     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1260     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1261     (?L gnus-tmp-lines ?s)
1262     (?O gnus-tmp-downloaded ?c)
1263     (?I gnus-tmp-indentation ?s)
1264     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1265     (?R gnus-tmp-replied ?c)
1266     (?\[ gnus-tmp-opening-bracket ?c)
1267     (?\] gnus-tmp-closing-bracket ?c)
1268     (?\> (make-string gnus-tmp-level ? ) ?s)
1269     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1270     (?i gnus-tmp-score ?d)
1271     (?z gnus-tmp-score-char ?c)
1272     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1273     (?U gnus-tmp-unread ?c)
1274     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1275         ?s)
1276     (?t (gnus-summary-number-of-articles-in-thread
1277          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1278         ?d)
1279     (?e (gnus-summary-number-of-articles-in-thread
1280          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1281         ?c)
1282     (?u gnus-tmp-user-defined ?s)
1283     (?P (gnus-pick-line-number) ?d)
1284     (?B gnus-tmp-thread-tree-header-string ?s)
1285     (user-date (gnus-user-date
1286                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1287   "An alist of format specifications that can appear in summary lines.
1288 These are paired with what variables they correspond with, along with
1289 the type of the variable (string, integer, character, etc).")
1290
1291 (defvar gnus-summary-dummy-line-format-alist
1292   `((?S gnus-tmp-subject ?s)
1293     (?N gnus-tmp-number ?d)
1294     (?u gnus-tmp-user-defined ?s)))
1295
1296 (defvar gnus-summary-mode-line-format-alist
1297   `((?G gnus-tmp-group-name ?s)
1298     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1299     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1300     (?A gnus-tmp-article-number ?d)
1301     (?Z gnus-tmp-unread-and-unselected ?s)
1302     (?V gnus-version ?s)
1303     (?U gnus-tmp-unread-and-unticked ?d)
1304     (?S gnus-tmp-subject ?s)
1305     (?e gnus-tmp-unselected ?d)
1306     (?u gnus-tmp-user-defined ?s)
1307     (?d (length gnus-newsgroup-dormant) ?d)
1308     (?t (length gnus-newsgroup-marked) ?d)
1309     (?h (length gnus-newsgroup-spam-marked) ?d)
1310     (?r (length gnus-newsgroup-reads) ?d)
1311     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1312     (?E gnus-newsgroup-expunged-tally ?d)
1313     (?s (gnus-current-score-file-nondirectory) ?s)))
1314
1315 (defvar gnus-last-search-regexp nil
1316   "Default regexp for article search command.")
1317
1318 (defvar gnus-last-shell-command nil
1319   "Default shell command on article.")
1320
1321 (defvar gnus-newsgroup-agentized nil
1322   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1323 (defvar gnus-newsgroup-begin nil)
1324 (defvar gnus-newsgroup-end nil)
1325 (defvar gnus-newsgroup-last-rmail nil)
1326 (defvar gnus-newsgroup-last-mail nil)
1327 (defvar gnus-newsgroup-last-folder nil)
1328 (defvar gnus-newsgroup-last-file nil)
1329 (defvar gnus-newsgroup-last-directory nil)
1330 (defvar gnus-newsgroup-auto-expire nil)
1331 (defvar gnus-newsgroup-active nil)
1332
1333 (defvar gnus-newsgroup-data nil)
1334 (defvar gnus-newsgroup-data-reverse nil)
1335 (defvar gnus-newsgroup-limit nil)
1336 (defvar gnus-newsgroup-limits nil)
1337 (defvar gnus-summary-use-undownloaded-faces nil)
1338
1339 (defvar gnus-newsgroup-unreads nil
1340   "Sorted list of unread articles in the current newsgroup.")
1341
1342 (defvar gnus-newsgroup-unselected nil
1343   "Sorted list of unselected unread articles in the current newsgroup.")
1344
1345 (defvar gnus-newsgroup-reads nil
1346   "Alist of read articles and article marks in the current newsgroup.")
1347
1348 (defvar gnus-newsgroup-expunged-tally nil)
1349
1350 (defvar gnus-newsgroup-marked nil
1351   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1352
1353 (defvar gnus-newsgroup-spam-marked nil
1354   "List of ranges of articles that have been marked as spam.")
1355
1356 (defvar gnus-newsgroup-killed nil
1357   "List of ranges of articles that have been through the scoring process.")
1358
1359 (defvar gnus-newsgroup-cached nil
1360   "Sorted list of articles that come from the article cache.")
1361
1362 (defvar gnus-newsgroup-saved nil
1363   "List of articles that have been saved.")
1364
1365 (defvar gnus-newsgroup-kill-headers nil)
1366
1367 (defvar gnus-newsgroup-replied nil
1368   "List of articles that have been replied to in the current newsgroup.")
1369
1370 (defvar gnus-newsgroup-forwarded nil
1371   "List of articles that have been forwarded in the current newsgroup.")
1372
1373 (defvar gnus-newsgroup-recent nil
1374   "List of articles that have are recent in the current newsgroup.")
1375
1376 (defvar gnus-newsgroup-expirable nil
1377   "Sorted list of articles in the current newsgroup that can be expired.")
1378
1379 (defvar gnus-newsgroup-processable nil
1380   "List of articles in the current newsgroup that can be processed.")
1381
1382 (defvar gnus-newsgroup-downloadable nil
1383   "Sorted list of articles in the current newsgroup that can be processed.")
1384
1385 (defvar gnus-newsgroup-unfetched nil
1386   "Sorted list of articles in the current newsgroup whose headers have
1387 not been fetched into the agent.
1388
1389 This list will always be a subset of gnus-newsgroup-undownloaded.")
1390
1391 (defvar gnus-newsgroup-undownloaded nil
1392   "List of articles in the current newsgroup that haven't been downloaded.")
1393
1394 (defvar gnus-newsgroup-unsendable nil
1395   "List of articles in the current newsgroup that won't be sent.")
1396
1397 (defvar gnus-newsgroup-bookmarks nil
1398   "List of articles in the current newsgroup that have bookmarks.")
1399
1400 (defvar gnus-newsgroup-dormant nil
1401   "Sorted list of dormant articles in the current newsgroup.")
1402
1403 (defvar gnus-newsgroup-unseen nil
1404   "List of unseen articles in the current newsgroup.")
1405
1406 (defvar gnus-newsgroup-seen nil
1407   "Range of seen articles in the current newsgroup.")
1408
1409 (defvar gnus-newsgroup-articles nil
1410   "List of articles in the current newsgroup.")
1411
1412 (defvar gnus-newsgroup-scored nil
1413   "List of scored articles in the current newsgroup.")
1414
1415 (defvar gnus-newsgroup-headers nil
1416   "List of article headers in the current newsgroup.")
1417
1418 (defvar gnus-newsgroup-threads nil)
1419
1420 (defvar gnus-newsgroup-prepared nil
1421   "Whether the current group has been prepared properly.")
1422
1423 (defvar gnus-newsgroup-ancient nil
1424   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1425
1426 (defvar gnus-newsgroup-sparse nil)
1427
1428 (defvar gnus-current-article nil)
1429 (defvar gnus-article-current nil)
1430 (defvar gnus-current-headers nil)
1431 (defvar gnus-have-all-headers nil)
1432 (defvar gnus-last-article nil)
1433 (defvar gnus-newsgroup-history nil)
1434 (defvar gnus-newsgroup-charset nil)
1435 (defvar gnus-newsgroup-ephemeral-charset nil)
1436 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1437
1438 (defvar gnus-article-before-search nil)
1439
1440 (defvar gnus-summary-local-variables
1441   '(gnus-newsgroup-name
1442     gnus-newsgroup-begin gnus-newsgroup-end
1443     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1444     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1445     gnus-newsgroup-last-directory
1446     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1447     gnus-newsgroup-unselected gnus-newsgroup-marked
1448     gnus-newsgroup-spam-marked
1449     gnus-newsgroup-reads gnus-newsgroup-saved
1450     gnus-newsgroup-replied gnus-newsgroup-forwarded
1451     gnus-newsgroup-recent
1452     gnus-newsgroup-expirable
1453     gnus-newsgroup-processable gnus-newsgroup-killed
1454     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1455     gnus-newsgroup-unfetched
1456     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1457     gnus-newsgroup-seen gnus-newsgroup-articles
1458     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1459     gnus-newsgroup-headers gnus-newsgroup-threads
1460     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1461     gnus-current-article gnus-current-headers gnus-have-all-headers
1462     gnus-last-article gnus-article-internal-prepare-hook
1463     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1464     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1465     gnus-thread-expunge-below
1466     gnus-score-alist gnus-current-score-file
1467     (gnus-summary-expunge-below . global)
1468     (gnus-summary-mark-below . global)
1469     (gnus-orphan-score . global)
1470     gnus-newsgroup-active gnus-scores-exclude-files
1471     gnus-newsgroup-history gnus-newsgroup-ancient
1472     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1473     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1474     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1475     (gnus-newsgroup-expunged-tally . 0)
1476     gnus-cache-removable-articles gnus-newsgroup-cached
1477     gnus-newsgroup-data gnus-newsgroup-data-reverse
1478     gnus-newsgroup-limit gnus-newsgroup-limits
1479     gnus-newsgroup-charset gnus-newsgroup-display
1480     gnus-summary-use-undownloaded-faces)
1481   "Variables that are buffer-local to the summary buffers.")
1482
1483 (defvar gnus-newsgroup-variables nil
1484   "A list of variables that have separate values in different newsgroups.
1485 A list of newsgroup (summary buffer) local variables, or cons of
1486 variables and their default expressions to be evalled (when the default
1487 values are not nil), that should be made global while the summary buffer
1488 is active.
1489
1490 Note: The default expressions will be evaluated (using function `eval')
1491 before assignment to the local variable rather than just assigned to it.
1492 If the default expression is the symbol `global', that symbol will not
1493 be evaluated but the global value of the local variable will be used
1494 instead.
1495
1496 These variables can be used to set variables in the group parameters
1497 while still allowing them to affect operations done in other buffers.
1498 For example:
1499
1500 \(setq gnus-newsgroup-variables
1501      '(message-use-followup-to
1502        (gnus-visible-headers .
1503          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1504 ")
1505
1506 ;; Byte-compiler warning.
1507 (eval-when-compile
1508   ;; Bind features so that require will believe that gnus-sum has
1509   ;; already been loaded (avoids infinite recursion)
1510   (let ((features (cons 'gnus-sum features)))
1511     ;; Several of the declarations in gnus-sum are needed to load the
1512     ;; following files. Right now, these definitions have been
1513     ;; compiled but not defined (evaluated).  We could either do a
1514     ;; eval-and-compile about all of the declarations or evaluate the
1515     ;; source file.
1516     (if (boundp 'gnus-newsgroup-variables)
1517         nil
1518       (load "gnus-sum.el" t t t))
1519     (require 'gnus)
1520     (require 'gnus-art)))
1521
1522 ;; MIME stuff.
1523
1524 (defvar gnus-decode-encoded-word-methods
1525   '(mail-decode-encoded-word-string)
1526   "List of methods used to decode encoded words.
1527
1528 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1529 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1530 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1531 whose names match REGEXP.
1532
1533 For example:
1534 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1535  mail-decode-encoded-word-string
1536  (\"chinese\" . rfc1843-decode-string))")
1537
1538 (defvar gnus-decode-encoded-word-methods-cache nil)
1539
1540 (defun gnus-multi-decode-encoded-word-string (string)
1541   "Apply the functions from `gnus-encoded-word-methods' that match."
1542   (unless (and gnus-decode-encoded-word-methods-cache
1543                (eq gnus-newsgroup-name
1544                    (car gnus-decode-encoded-word-methods-cache)))
1545     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1546     (mapcar (lambda (x)
1547               (if (symbolp x)
1548                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1549                 (if (and gnus-newsgroup-name
1550                          (string-match (car x) gnus-newsgroup-name))
1551                     (nconc gnus-decode-encoded-word-methods-cache
1552                            (list (cdr x))))))
1553             gnus-decode-encoded-word-methods))
1554   (let ((xlist gnus-decode-encoded-word-methods-cache))
1555     (pop xlist)
1556     (while xlist
1557       (setq string (funcall (pop xlist) string))))
1558   string)
1559
1560 ;; Subject simplification.
1561
1562 (defun gnus-simplify-whitespace (str)
1563   "Remove excessive whitespace from STR."
1564   ;; Multiple spaces.
1565   (while (string-match "[ \t][ \t]+" str)
1566     (setq str (concat (substring str 0 (match-beginning 0))
1567                         " "
1568                         (substring str (match-end 0)))))
1569   ;; Leading spaces.
1570   (when (string-match "^[ \t]+" str)
1571     (setq str (substring str (match-end 0))))
1572   ;; Trailing spaces.
1573   (when (string-match "[ \t]+$" str)
1574     (setq str (substring str 0 (match-beginning 0))))
1575   str)
1576
1577 (defun gnus-simplify-all-whitespace (str)
1578   "Remove all whitespace from STR."
1579   (while (string-match "[ \t\n]+" str)
1580     (setq str (replace-match "" nil nil str)))
1581   str)
1582
1583 (defsubst gnus-simplify-subject-re (subject)
1584   "Remove \"Re:\" from subject lines."
1585   (if (string-match message-subject-re-regexp subject)
1586       (substring subject (match-end 0))
1587     subject))
1588
1589 (defun gnus-simplify-subject (subject &optional re-only)
1590   "Remove `Re:' and words in parentheses.
1591 If RE-ONLY is non-nil, strip leading `Re:'s only."
1592   (let ((case-fold-search t))           ;Ignore case.
1593     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1594     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1595       (setq subject (substring subject (match-end 0))))
1596     ;; Remove uninteresting prefixes.
1597     (when (and (not re-only)
1598                gnus-simplify-ignored-prefixes
1599                (string-match gnus-simplify-ignored-prefixes subject))
1600       (setq subject (substring subject (match-end 0))))
1601     ;; Remove words in parentheses from end.
1602     (unless re-only
1603       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1604         (setq subject (substring subject 0 (match-beginning 0)))))
1605     ;; Return subject string.
1606     subject))
1607
1608 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1609 ;; all whitespace.
1610 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1611   (goto-char (point-min))
1612   (while (re-search-forward regexp nil t)
1613     (replace-match (or newtext ""))))
1614
1615 (defun gnus-simplify-buffer-fuzzy ()
1616   "Simplify string in the buffer fuzzily.
1617 The string in the accessible portion of the current buffer is simplified.
1618 It is assumed to be a single-line subject.
1619 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1620 matter is removed.  Additional things can be deleted by setting
1621 `gnus-simplify-subject-fuzzy-regexp'."
1622   (let ((case-fold-search t)
1623         (modified-tick))
1624     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1625
1626     (while (not (eq modified-tick (buffer-modified-tick)))
1627       (setq modified-tick (buffer-modified-tick))
1628       (cond
1629        ((listp gnus-simplify-subject-fuzzy-regexp)
1630         (mapcar 'gnus-simplify-buffer-fuzzy-step
1631                 gnus-simplify-subject-fuzzy-regexp))
1632        (gnus-simplify-subject-fuzzy-regexp
1633         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1634       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1635       (gnus-simplify-buffer-fuzzy-step
1636        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1637       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1638
1639     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1640     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1641     (gnus-simplify-buffer-fuzzy-step " $")
1642     (gnus-simplify-buffer-fuzzy-step "^ +")))
1643
1644 (defun gnus-simplify-subject-fuzzy (subject)
1645   "Simplify a subject string fuzzily.
1646 See `gnus-simplify-buffer-fuzzy' for details."
1647   (save-excursion
1648     (gnus-set-work-buffer)
1649     (let ((case-fold-search t))
1650       ;; Remove uninteresting prefixes.
1651       (when (and gnus-simplify-ignored-prefixes
1652                  (string-match gnus-simplify-ignored-prefixes subject))
1653         (setq subject (substring subject (match-end 0))))
1654       (insert subject)
1655       (inline (gnus-simplify-buffer-fuzzy))
1656       (buffer-string))))
1657
1658 (defsubst gnus-simplify-subject-fully (subject)
1659   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1660   (cond
1661    (gnus-simplify-subject-functions
1662     (gnus-map-function gnus-simplify-subject-functions subject))
1663    ((null gnus-summary-gather-subject-limit)
1664     (gnus-simplify-subject-re subject))
1665    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1666     (gnus-simplify-subject-fuzzy subject))
1667    ((numberp gnus-summary-gather-subject-limit)
1668     (gnus-limit-string (gnus-simplify-subject-re subject)
1669                        gnus-summary-gather-subject-limit))
1670    (t
1671     subject)))
1672
1673 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1674   "Check whether two subjects are equal.
1675 If optional argument SIMPLE-FIRST is t, first argument is already
1676 simplified."
1677   (cond
1678    ((null simple-first)
1679     (equal (gnus-simplify-subject-fully s1)
1680            (gnus-simplify-subject-fully s2)))
1681    (t
1682     (equal s1
1683            (gnus-simplify-subject-fully s2)))))
1684
1685 (defun gnus-summary-bubble-group ()
1686   "Increase the score of the current group.
1687 This is a handy function to add to `gnus-summary-exit-hook' to
1688 increase the score of each group you read."
1689   (gnus-group-add-score gnus-newsgroup-name))
1690
1691 \f
1692 ;;;
1693 ;;; Gnus summary mode
1694 ;;;
1695
1696 (put 'gnus-summary-mode 'mode-class 'special)
1697
1698 (defvar gnus-article-commands-menu)
1699
1700 ;; Non-orthogonal keys
1701
1702 (gnus-define-keys gnus-summary-mode-map
1703   " " gnus-summary-next-page
1704   "\177" gnus-summary-prev-page
1705   [delete] gnus-summary-prev-page
1706   [backspace] gnus-summary-prev-page
1707   "\r" gnus-summary-scroll-up
1708   "\M-\r" gnus-summary-scroll-down
1709   "n" gnus-summary-next-unread-article
1710   "p" gnus-summary-prev-unread-article
1711   "N" gnus-summary-next-article
1712   "P" gnus-summary-prev-article
1713   "\M-\C-n" gnus-summary-next-same-subject
1714   "\M-\C-p" gnus-summary-prev-same-subject
1715   "\M-n" gnus-summary-next-unread-subject
1716   "\M-p" gnus-summary-prev-unread-subject
1717   "." gnus-summary-first-unread-article
1718   "," gnus-summary-best-unread-article
1719   "\M-s" gnus-summary-search-article-forward
1720   "\M-r" gnus-summary-search-article-backward
1721   "\M-S" gnus-summary-repeat-search-article-forward
1722   "\M-R" gnus-summary-repeat-search-article-backward
1723   "<" gnus-summary-beginning-of-article
1724   ">" gnus-summary-end-of-article
1725   "j" gnus-summary-goto-article
1726   "^" gnus-summary-refer-parent-article
1727   "\M-^" gnus-summary-refer-article
1728   "u" gnus-summary-tick-article-forward
1729   "!" gnus-summary-tick-article-forward
1730   "U" gnus-summary-tick-article-backward
1731   "d" gnus-summary-mark-as-read-forward
1732   "D" gnus-summary-mark-as-read-backward
1733   "E" gnus-summary-mark-as-expirable
1734   "\M-u" gnus-summary-clear-mark-forward
1735   "\M-U" gnus-summary-clear-mark-backward
1736   "k" gnus-summary-kill-same-subject-and-select
1737   "\C-k" gnus-summary-kill-same-subject
1738   "\M-\C-k" gnus-summary-kill-thread
1739   "\M-\C-l" gnus-summary-lower-thread
1740   "e" gnus-summary-edit-article
1741   "#" gnus-summary-mark-as-processable
1742   "\M-#" gnus-summary-unmark-as-processable
1743   "\M-\C-t" gnus-summary-toggle-threads
1744   "\M-\C-s" gnus-summary-show-thread
1745   "\M-\C-h" gnus-summary-hide-thread
1746   "\M-\C-f" gnus-summary-next-thread
1747   "\M-\C-b" gnus-summary-prev-thread
1748   [(meta down)] gnus-summary-next-thread
1749   [(meta up)] gnus-summary-prev-thread
1750   "\M-\C-u" gnus-summary-up-thread
1751   "\M-\C-d" gnus-summary-down-thread
1752   "&" gnus-summary-execute-command
1753   "c" gnus-summary-catchup-and-exit
1754   "\C-w" gnus-summary-mark-region-as-read
1755   "\C-t" gnus-summary-toggle-truncation
1756   "?" gnus-summary-mark-as-dormant
1757   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1758   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1759   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1760   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1761   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1762   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1763   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1764   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1765   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1766   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1767   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1768   "=" gnus-summary-expand-window
1769   "\C-x\C-s" gnus-summary-reselect-current-group
1770   "\M-g" gnus-summary-rescan-group
1771   "w" gnus-summary-stop-page-breaking
1772   "\C-c\C-r" gnus-summary-caesar-message
1773   "f" gnus-summary-followup
1774   "F" gnus-summary-followup-with-original
1775   "C" gnus-summary-cancel-article
1776   "r" gnus-summary-reply
1777   "R" gnus-summary-reply-with-original
1778   "\C-c\C-f" gnus-summary-mail-forward
1779   "o" gnus-summary-save-article
1780   "\C-o" gnus-summary-save-article-mail
1781   "|" gnus-summary-pipe-output
1782   "\M-k" gnus-summary-edit-local-kill
1783   "\M-K" gnus-summary-edit-global-kill
1784   ;; "V" gnus-version
1785   "\C-c\C-d" gnus-summary-describe-group
1786   "q" gnus-summary-exit
1787   "Q" gnus-summary-exit-no-update
1788   "\C-c\C-i" gnus-info-find-node
1789   gnus-mouse-2 gnus-mouse-pick-article
1790   [follow-link] mouse-face
1791   "m" gnus-summary-mail-other-window
1792   "a" gnus-summary-post-news
1793   "i" gnus-summary-news-other-window
1794   "x" gnus-summary-limit-to-unread
1795   "s" gnus-summary-isearch-article
1796   "t" gnus-summary-toggle-header
1797   "g" gnus-summary-show-article
1798   "l" gnus-summary-goto-last-article
1799   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1800   "\C-d" gnus-summary-enter-digest-group
1801   "\M-\C-d" gnus-summary-read-document
1802   "\M-\C-e" gnus-summary-edit-parameters
1803   "\M-\C-a" gnus-summary-customize-parameters
1804   "\C-c\C-b" gnus-bug
1805   "*" gnus-cache-enter-article
1806   "\M-*" gnus-cache-remove-article
1807   "\M-&" gnus-summary-universal-argument
1808   "\C-l" gnus-recenter
1809   "I" gnus-summary-increase-score
1810   "L" gnus-summary-lower-score
1811   "\M-i" gnus-symbolic-argument
1812   "h" gnus-summary-select-article-buffer
1813
1814   "b" gnus-article-view-part
1815   "\M-t" gnus-summary-toggle-display-buttonized
1816
1817   "V" gnus-summary-score-map
1818   "X" gnus-uu-extract-map
1819   "S" gnus-summary-send-map)
1820
1821 ;; Sort of orthogonal keymap
1822 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1823   "t" gnus-summary-tick-article-forward
1824   "!" gnus-summary-tick-article-forward
1825   "d" gnus-summary-mark-as-read-forward
1826   "r" gnus-summary-mark-as-read-forward
1827   "c" gnus-summary-clear-mark-forward
1828   " " gnus-summary-clear-mark-forward
1829   "e" gnus-summary-mark-as-expirable
1830   "x" gnus-summary-mark-as-expirable
1831   "?" gnus-summary-mark-as-dormant
1832   "b" gnus-summary-set-bookmark
1833   "B" gnus-summary-remove-bookmark
1834   "#" gnus-summary-mark-as-processable
1835   "\M-#" gnus-summary-unmark-as-processable
1836   "S" gnus-summary-limit-include-expunged
1837   "C" gnus-summary-catchup
1838   "H" gnus-summary-catchup-to-here
1839   "h" gnus-summary-catchup-from-here
1840   "\C-c" gnus-summary-catchup-all
1841   "k" gnus-summary-kill-same-subject-and-select
1842   "K" gnus-summary-kill-same-subject
1843   "P" gnus-uu-mark-map)
1844
1845 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1846   "c" gnus-summary-clear-above
1847   "u" gnus-summary-tick-above
1848   "m" gnus-summary-mark-above
1849   "k" gnus-summary-kill-below)
1850
1851 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1852   "/" gnus-summary-limit-to-subject
1853   "n" gnus-summary-limit-to-articles
1854   "b" gnus-summary-limit-to-bodies
1855   "w" gnus-summary-pop-limit
1856   "s" gnus-summary-limit-to-subject
1857   "a" gnus-summary-limit-to-author
1858   "u" gnus-summary-limit-to-unread
1859   "m" gnus-summary-limit-to-marks
1860   "M" gnus-summary-limit-exclude-marks
1861   "v" gnus-summary-limit-to-score
1862   "*" gnus-summary-limit-include-cached
1863   "D" gnus-summary-limit-include-dormant
1864   "T" gnus-summary-limit-include-thread
1865   "d" gnus-summary-limit-exclude-dormant
1866   "t" gnus-summary-limit-to-age
1867   "." gnus-summary-limit-to-unseen
1868   "x" gnus-summary-limit-to-extra
1869   "p" gnus-summary-limit-to-display-predicate
1870   "E" gnus-summary-limit-include-expunged
1871   "c" gnus-summary-limit-exclude-childless-dormant
1872   "C" gnus-summary-limit-mark-excluded-as-read
1873   "o" gnus-summary-insert-old-articles
1874   "N" gnus-summary-insert-new-articles
1875   "r" gnus-summary-limit-to-replied
1876   "R" gnus-summary-limit-to-recipient)
1877
1878 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1879   "n" gnus-summary-next-unread-article
1880   "p" gnus-summary-prev-unread-article
1881   "N" gnus-summary-next-article
1882   "P" gnus-summary-prev-article
1883   "\C-n" gnus-summary-next-same-subject
1884   "\C-p" gnus-summary-prev-same-subject
1885   "\M-n" gnus-summary-next-unread-subject
1886   "\M-p" gnus-summary-prev-unread-subject
1887   "f" gnus-summary-first-unread-article
1888   "b" gnus-summary-best-unread-article
1889   "j" gnus-summary-goto-article
1890   "g" gnus-summary-goto-subject
1891   "l" gnus-summary-goto-last-article
1892   "o" gnus-summary-pop-article)
1893
1894 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1895   "k" gnus-summary-kill-thread
1896   "l" gnus-summary-lower-thread
1897   "i" gnus-summary-raise-thread
1898   "T" gnus-summary-toggle-threads
1899   "t" gnus-summary-rethread-current
1900   "^" gnus-summary-reparent-thread
1901   "\M-^" gnus-summary-reparent-children
1902   "s" gnus-summary-show-thread
1903   "S" gnus-summary-show-all-threads
1904   "h" gnus-summary-hide-thread
1905   "H" gnus-summary-hide-all-threads
1906   "n" gnus-summary-next-thread
1907   "p" gnus-summary-prev-thread
1908   "u" gnus-summary-up-thread
1909   "o" gnus-summary-top-thread
1910   "d" gnus-summary-down-thread
1911   "#" gnus-uu-mark-thread
1912   "\M-#" gnus-uu-unmark-thread)
1913
1914 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1915   "g" gnus-summary-prepare
1916   "c" gnus-summary-insert-cached-articles
1917   "d" gnus-summary-insert-dormant-articles)
1918
1919 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1920   "c" gnus-summary-catchup-and-exit
1921   "C" gnus-summary-catchup-all-and-exit
1922   "E" gnus-summary-exit-no-update
1923   "Q" gnus-summary-exit
1924   "Z" gnus-summary-exit
1925   "n" gnus-summary-catchup-and-goto-next-group
1926   "p" gnus-summary-catchup-and-goto-prev-group
1927   "R" gnus-summary-reselect-current-group
1928   "G" gnus-summary-rescan-group
1929   "N" gnus-summary-next-group
1930   "s" gnus-summary-save-newsrc
1931   "P" gnus-summary-prev-group)
1932
1933 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1934   " " gnus-summary-next-page
1935   "n" gnus-summary-next-page
1936   "\177" gnus-summary-prev-page
1937   [delete] gnus-summary-prev-page
1938   "p" gnus-summary-prev-page
1939   "\r" gnus-summary-scroll-up
1940   "\M-\r" gnus-summary-scroll-down
1941   "<" gnus-summary-beginning-of-article
1942   ">" gnus-summary-end-of-article
1943   "b" gnus-summary-beginning-of-article
1944   "e" gnus-summary-end-of-article
1945   "^" gnus-summary-refer-parent-article
1946   "r" gnus-summary-refer-parent-article
1947   "D" gnus-summary-enter-digest-group
1948   "R" gnus-summary-refer-references
1949   "T" gnus-summary-refer-thread
1950   "g" gnus-summary-show-article
1951   "s" gnus-summary-isearch-article
1952   "P" gnus-summary-print-article
1953   "M" gnus-mailing-list-insinuate
1954   "t" gnus-article-babel)
1955
1956 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1957   "b" gnus-article-add-buttons
1958   "B" gnus-article-add-buttons-to-head
1959   "o" gnus-article-treat-overstrike
1960   "e" gnus-article-emphasize
1961   "w" gnus-article-fill-cited-article
1962   "Q" gnus-article-fill-long-lines
1963   "L" gnus-article-toggle-truncate-lines
1964   "C" gnus-article-capitalize-sentences
1965   "c" gnus-article-remove-cr
1966   "q" gnus-article-de-quoted-unreadable
1967   "6" gnus-article-de-base64-unreadable
1968   "Z" gnus-article-decode-HZ
1969   "A" gnus-article-treat-ansi-sequences
1970   "h" gnus-article-wash-html
1971   "u" gnus-article-unsplit-urls
1972   "s" gnus-summary-force-verify-and-decrypt
1973   "f" gnus-article-display-x-face
1974   "l" gnus-summary-stop-page-breaking
1975   "r" gnus-summary-caesar-message
1976   "m" gnus-summary-morse-message
1977   "t" gnus-summary-toggle-header
1978   "g" gnus-treat-smiley
1979   "v" gnus-summary-verbose-headers
1980   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1981   "p" gnus-article-verify-x-pgp-sig
1982   "d" gnus-article-treat-dumbquotes
1983   "i" gnus-summary-idna-message)
1984
1985 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1986   ;; mnemonic: deuglif*Y*
1987   "u" gnus-article-outlook-unwrap-lines
1988   "a" gnus-article-outlook-repair-attribution
1989   "c" gnus-article-outlook-rearrange-citation
1990   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1991
1992 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1993   "a" gnus-article-hide
1994   "h" gnus-article-hide-headers
1995   "b" gnus-article-hide-boring-headers
1996   "s" gnus-article-hide-signature
1997   "c" gnus-article-hide-citation
1998   "C" gnus-article-hide-citation-in-followups
1999   "l" gnus-article-hide-list-identifiers
2000   "B" gnus-article-strip-banner
2001   "P" gnus-article-hide-pem
2002   "\C-c" gnus-article-hide-citation-maybe)
2003
2004 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2005   "a" gnus-article-highlight
2006   "h" gnus-article-highlight-headers
2007   "c" gnus-article-highlight-citation
2008   "s" gnus-article-highlight-signature)
2009
2010 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2011   "f" gnus-article-treat-fold-headers
2012   "u" gnus-article-treat-unfold-headers
2013   "n" gnus-article-treat-fold-newsgroups)
2014
2015 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2016   "x" gnus-article-display-x-face
2017   "d" gnus-article-display-face
2018   "s" gnus-treat-smiley
2019   "D" gnus-article-remove-images
2020   "f" gnus-treat-from-picon
2021   "m" gnus-treat-mail-picon
2022   "n" gnus-treat-newsgroups-picon)
2023
2024 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2025   "w" gnus-article-decode-mime-words
2026   "c" gnus-article-decode-charset
2027   "v" gnus-mime-view-all-parts
2028   "b" gnus-article-view-part)
2029
2030 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2031   "z" gnus-article-date-ut
2032   "u" gnus-article-date-ut
2033   "l" gnus-article-date-local
2034   "p" gnus-article-date-english
2035   "e" gnus-article-date-lapsed
2036   "o" gnus-article-date-original
2037   "i" gnus-article-date-iso8601
2038   "s" gnus-article-date-user)
2039
2040 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2041   "t" gnus-article-remove-trailing-blank-lines
2042   "l" gnus-article-strip-leading-blank-lines
2043   "m" gnus-article-strip-multiple-blank-lines
2044   "a" gnus-article-strip-blank-lines
2045   "A" gnus-article-strip-all-blank-lines
2046   "s" gnus-article-strip-leading-space
2047   "e" gnus-article-strip-trailing-space
2048   "w" gnus-article-remove-leading-whitespace)
2049
2050 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2051   "v" gnus-version
2052   "f" gnus-summary-fetch-faq
2053   "d" gnus-summary-describe-group
2054   "h" gnus-summary-describe-briefly
2055   "i" gnus-info-find-node
2056   "c" gnus-group-fetch-charter
2057   "C" gnus-group-fetch-control)
2058
2059 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2060   "e" gnus-summary-expire-articles
2061   "\M-\C-e" gnus-summary-expire-articles-now
2062   "\177" gnus-summary-delete-article
2063   [delete] gnus-summary-delete-article
2064   [backspace] gnus-summary-delete-article
2065   "m" gnus-summary-move-article
2066   "r" gnus-summary-respool-article
2067   "w" gnus-summary-edit-article
2068   "c" gnus-summary-copy-article
2069   "B" gnus-summary-crosspost-article
2070   "q" gnus-summary-respool-query
2071   "t" gnus-summary-respool-trace
2072   "i" gnus-summary-import-article
2073   "I" gnus-summary-create-article
2074   "p" gnus-summary-article-posted-p)
2075
2076 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2077   "o" gnus-summary-save-article
2078   "m" gnus-summary-save-article-mail
2079   "F" gnus-summary-write-article-file
2080   "r" gnus-summary-save-article-rmail
2081   "f" gnus-summary-save-article-file
2082   "b" gnus-summary-save-article-body-file
2083   "B" gnus-summary-write-article-body-file
2084   "h" gnus-summary-save-article-folder
2085   "v" gnus-summary-save-article-vm
2086   "p" gnus-summary-pipe-output
2087   "P" gnus-summary-muttprint
2088   "s" gnus-soup-add-article)
2089
2090 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2091   "b" gnus-summary-display-buttonized
2092   "m" gnus-summary-repair-multipart
2093   "v" gnus-article-view-part
2094   "o" gnus-article-save-part
2095   "O" gnus-article-save-part-and-strip
2096   "r" gnus-article-replace-part
2097   "d" gnus-article-delete-part
2098   "t" gnus-article-view-part-as-type
2099   "j" gnus-article-jump-to-part
2100   "c" gnus-article-copy-part
2101   "C" gnus-article-view-part-as-charset
2102   "e" gnus-article-view-part-externally
2103   "H" gnus-article-browse-html-article
2104   "E" gnus-article-encrypt-body
2105   "i" gnus-article-inline-part
2106   "|" gnus-article-pipe-part)
2107
2108 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2109   "p" gnus-summary-mark-as-processable
2110   "u" gnus-summary-unmark-as-processable
2111   "U" gnus-summary-unmark-all-processable
2112   "v" gnus-uu-mark-over
2113   "s" gnus-uu-mark-series
2114   "r" gnus-uu-mark-region
2115   "g" gnus-uu-unmark-region
2116   "R" gnus-uu-mark-by-regexp
2117   "G" gnus-uu-unmark-by-regexp
2118   "t" gnus-uu-mark-thread
2119   "T" gnus-uu-unmark-thread
2120   "a" gnus-uu-mark-all
2121   "b" gnus-uu-mark-buffer
2122   "S" gnus-uu-mark-sparse
2123   "k" gnus-summary-kill-process-mark
2124   "y" gnus-summary-yank-process-mark
2125   "w" gnus-summary-save-process-mark
2126   "i" gnus-uu-invert-processable)
2127
2128 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2129   ;;"x" gnus-uu-extract-any
2130   "m" gnus-summary-save-parts
2131   "u" gnus-uu-decode-uu
2132   "U" gnus-uu-decode-uu-and-save
2133   "s" gnus-uu-decode-unshar
2134   "S" gnus-uu-decode-unshar-and-save
2135   "o" gnus-uu-decode-save
2136   "O" gnus-uu-decode-save
2137   "b" gnus-uu-decode-binhex
2138   "B" gnus-uu-decode-binhex
2139   "p" gnus-uu-decode-postscript
2140   "P" gnus-uu-decode-postscript-and-save)
2141
2142 (gnus-define-keys
2143     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2144   "u" gnus-uu-decode-uu-view
2145   "U" gnus-uu-decode-uu-and-save-view
2146   "s" gnus-uu-decode-unshar-view
2147   "S" gnus-uu-decode-unshar-and-save-view
2148   "o" gnus-uu-decode-save-view
2149   "O" gnus-uu-decode-save-view
2150   "b" gnus-uu-decode-binhex-view
2151   "B" gnus-uu-decode-binhex-view
2152   "p" gnus-uu-decode-postscript-view
2153   "P" gnus-uu-decode-postscript-and-save-view)
2154
2155 (defvar gnus-article-post-menu nil)
2156
2157 (defconst gnus-summary-menu-maxlen 20)
2158
2159 (defun gnus-summary-menu-split (menu)
2160   ;; If we have lots of elements, divide them into groups of 20
2161   ;; and make a pane (or submenu) for each one.
2162   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2163       (let ((menu menu) sublists next
2164             (i 1))
2165         (while menu
2166           ;; Pull off the next gnus-summary-menu-maxlen elements
2167           ;; and make them the next element of sublist.
2168           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2169           (if next
2170               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2171                       nil))
2172           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2173                                              (aref (car (last menu)) 0)) menu)
2174                                sublists))
2175           (setq i (1+ i))
2176           (setq menu next))
2177         (nreverse sublists))
2178     ;; Few elements--put them all in one pane.
2179     menu))
2180
2181 (defun gnus-summary-make-menu-bar ()
2182   (gnus-turn-off-edit-menu 'summary)
2183
2184   (unless (boundp 'gnus-summary-misc-menu)
2185
2186     (easy-menu-define
2187       gnus-summary-kill-menu gnus-summary-mode-map ""
2188       (cons
2189        "Score"
2190        (nconc
2191         (list
2192          ["Customize" gnus-score-customize t])
2193         (gnus-make-score-map 'increase)
2194         (gnus-make-score-map 'lower)
2195         '(("Mark"
2196            ["Kill below" gnus-summary-kill-below t]
2197            ["Mark above" gnus-summary-mark-above t]
2198            ["Tick above" gnus-summary-tick-above t]
2199            ["Clear above" gnus-summary-clear-above t])
2200           ["Current score" gnus-summary-current-score t]
2201           ["Set score" gnus-summary-set-score t]
2202           ["Switch current score file..." gnus-score-change-score-file t]
2203           ["Set mark below..." gnus-score-set-mark-below t]
2204           ["Set expunge below..." gnus-score-set-expunge-below t]
2205           ["Edit current score file" gnus-score-edit-current-scores t]
2206           ["Edit score file" gnus-score-edit-file t]
2207           ["Trace score" gnus-score-find-trace t]
2208           ["Find words" gnus-score-find-favourite-words t]
2209           ["Rescore buffer" gnus-summary-rescore t]
2210           ["Increase score..." gnus-summary-increase-score t]
2211           ["Lower score..." gnus-summary-lower-score t]))))
2212
2213     ;; Define both the Article menu in the summary buffer and the
2214     ;; equivalent Commands menu in the article buffer here for
2215     ;; consistency.
2216     (let ((innards
2217            `(("Hide"
2218               ["All" gnus-article-hide t]
2219               ["Headers" gnus-article-hide-headers t]
2220               ["Signature" gnus-article-hide-signature t]
2221               ["Citation" gnus-article-hide-citation t]
2222               ["List identifiers" gnus-article-hide-list-identifiers t]
2223               ["Banner" gnus-article-strip-banner t]
2224               ["Boring headers" gnus-article-hide-boring-headers t])
2225              ("Highlight"
2226               ["All" gnus-article-highlight t]
2227               ["Headers" gnus-article-highlight-headers t]
2228               ["Signature" gnus-article-highlight-signature t]
2229               ["Citation" gnus-article-highlight-citation t])
2230              ("MIME"
2231               ["Words" gnus-article-decode-mime-words t]
2232               ["Charset" gnus-article-decode-charset t]
2233               ["QP" gnus-article-de-quoted-unreadable t]
2234               ["Base64" gnus-article-de-base64-unreadable t]
2235               ["View MIME buttons" gnus-summary-display-buttonized t]
2236               ["View all" gnus-mime-view-all-parts t]
2237               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2238               ["Encrypt body" gnus-article-encrypt-body
2239                :active (not (gnus-group-read-only-p))
2240                ,@(if (featurep 'xemacs) nil
2241                    '(:help "Encrypt the message body on disk"))]
2242               ["Extract all parts..." gnus-summary-save-parts t]
2243               ("Multipart"
2244                ["Repair multipart" gnus-summary-repair-multipart t]
2245                ["Pipe part..." gnus-article-pipe-part t]
2246                ["Inline part" gnus-article-inline-part t]
2247                ["View part as type..." gnus-article-view-part-as-type t]
2248                ["Encrypt body" gnus-article-encrypt-body
2249                 :active (not (gnus-group-read-only-p))
2250                ,@(if (featurep 'xemacs) nil
2251                    '(:help "Encrypt the message body on disk"))]
2252                ["View part externally" gnus-article-view-part-externally t]
2253                ["View HTML parts in browser" gnus-article-browse-html-article t]
2254                ["View part with charset..." gnus-article-view-part-as-charset t]
2255                ["Copy part" gnus-article-copy-part t]
2256                ["Save part..." gnus-article-save-part t]
2257                ["View part" gnus-article-view-part t]))
2258              ("Date"
2259               ["Local" gnus-article-date-local t]
2260               ["ISO8601" gnus-article-date-iso8601 t]
2261               ["UT" gnus-article-date-ut t]
2262               ["Original" gnus-article-date-original t]
2263               ["Lapsed" gnus-article-date-lapsed t]
2264               ["User-defined" gnus-article-date-user t])
2265              ("Display"
2266               ["Remove images" gnus-article-remove-images t]
2267               ["Toggle smiley" gnus-treat-smiley t]
2268               ["Show X-Face" gnus-article-display-x-face t]
2269               ["Show picons in From" gnus-treat-from-picon t]
2270               ["Show picons in mail headers" gnus-treat-mail-picon t]
2271               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2272               ("View as different encoding"
2273                ,@(gnus-summary-menu-split
2274                   (mapcar
2275                    (lambda (cs)
2276                      ;; Since easymenu under Emacs doesn't allow
2277                      ;; lambda forms for menu commands, we should
2278                      ;; provide intern'ed function symbols.
2279                      (let ((command (intern (format "\
2280 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2281                        (fset command
2282                              `(lambda ()
2283                                 (interactive)
2284                                 (let ((gnus-summary-show-article-charset-alist
2285                                        '((1 . ,cs))))
2286                                   (gnus-summary-show-article 1))))
2287                        `[,(symbol-name cs) ,command t]))
2288                    (sort (if (fboundp 'coding-system-list)
2289                              (coding-system-list)
2290                            (mapcar 'car mm-mime-mule-charset-alist))
2291                          'string<)))))
2292              ("Washing"
2293               ("Remove Blanks"
2294                ["Leading" gnus-article-strip-leading-blank-lines t]
2295                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2296                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2297                ["All of the above" gnus-article-strip-blank-lines t]
2298                ["All" gnus-article-strip-all-blank-lines t]
2299                ["Leading space" gnus-article-strip-leading-space t]
2300                ["Trailing space" gnus-article-strip-trailing-space t]
2301                ["Leading space in headers"
2302                 gnus-article-remove-leading-whitespace t])
2303               ["Overstrike" gnus-article-treat-overstrike t]
2304               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2305               ["Emphasis" gnus-article-emphasize t]
2306               ["Word wrap" gnus-article-fill-cited-article t]
2307               ["Fill long lines" gnus-article-fill-long-lines t]
2308               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2309               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2310               ["Remove CR" gnus-article-remove-cr t]
2311               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2312               ["Base64" gnus-article-de-base64-unreadable t]
2313               ["Rot 13" gnus-summary-caesar-message
2314                ,@(if (featurep 'xemacs) '(t)
2315                    '(:help "\"Caesar rotate\" article by 13"))]
2316               ["De-IDNA" gnus-summary-idna-message t]
2317               ["Morse decode" gnus-summary-morse-message t]
2318               ["Unix pipe..." gnus-summary-pipe-message t]
2319               ["Add buttons" gnus-article-add-buttons t]
2320               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2321               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2322               ["Verbose header" gnus-summary-verbose-headers t]
2323               ["Toggle header" gnus-summary-toggle-header t]
2324               ["Unfold headers" gnus-article-treat-unfold-headers t]
2325               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2326               ["Html" gnus-article-wash-html t]
2327               ["Unsplit URLs" gnus-article-unsplit-urls t]
2328               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2329               ["Decode HZ" gnus-article-decode-HZ t]
2330               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2331               ("(Outlook) Deuglify"
2332                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2333                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2334                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2335                ["Full (Outlook) deuglify"
2336                 gnus-article-outlook-deuglify-article t])
2337               )
2338              ("Output"
2339               ["Save in default format..." gnus-summary-save-article
2340                ,@(if (featurep 'xemacs) '(t)
2341                    '(:help "Save article using default method"))]
2342               ["Save in file..." gnus-summary-save-article-file
2343                ,@(if (featurep 'xemacs) '(t)
2344                    '(:help "Save article in file"))]
2345               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2346               ["Save in MH folder..." gnus-summary-save-article-folder t]
2347               ["Save in VM folder..." gnus-summary-save-article-vm t]
2348               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2349               ["Save body in file..." gnus-summary-save-article-body-file t]
2350               ["Pipe through a filter..." gnus-summary-pipe-output t]
2351               ["Add to SOUP packet" gnus-soup-add-article t]
2352               ["Print with Muttprint..." gnus-summary-muttprint t]
2353               ["Print" gnus-summary-print-article
2354                ,@(if (featurep 'xemacs) '(t)
2355                    '(:help "Generate and print a PostScript image"))])
2356              ("Copy, move,... (Backend)"
2357               ,@(if (featurep 'xemacs) nil
2358                   '(:help "Copying, moving, expiring articles..."))
2359               ["Respool article..." gnus-summary-respool-article t]
2360               ["Move article..." gnus-summary-move-article
2361                (gnus-check-backend-function
2362                 'request-move-article gnus-newsgroup-name)]
2363               ["Copy article..." gnus-summary-copy-article t]
2364               ["Crosspost article..." gnus-summary-crosspost-article
2365                (gnus-check-backend-function
2366                 'request-replace-article gnus-newsgroup-name)]
2367               ["Import file..." gnus-summary-import-article
2368                (gnus-check-backend-function
2369                 'request-accept-article gnus-newsgroup-name)]
2370               ["Create article..." gnus-summary-create-article
2371                (gnus-check-backend-function
2372                 'request-accept-article gnus-newsgroup-name)]
2373               ["Check if posted" gnus-summary-article-posted-p t]
2374               ["Edit article" gnus-summary-edit-article
2375                (not (gnus-group-read-only-p))]
2376               ["Delete article" gnus-summary-delete-article
2377                (gnus-check-backend-function
2378                 'request-expire-articles gnus-newsgroup-name)]
2379               ["Query respool" gnus-summary-respool-query t]
2380               ["Trace respool" gnus-summary-respool-trace t]
2381               ["Delete expirable articles" gnus-summary-expire-articles-now
2382                (gnus-check-backend-function
2383                 'request-expire-articles gnus-newsgroup-name)])
2384              ("Extract"
2385               ["Uudecode" gnus-uu-decode-uu
2386                ,@(if (featurep 'xemacs) '(t)
2387                    '(:help "Decode uuencoded article(s)"))]
2388               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2389               ["Unshar" gnus-uu-decode-unshar t]
2390               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2391               ["Save" gnus-uu-decode-save t]
2392               ["Binhex" gnus-uu-decode-binhex t]
2393               ["Postscript" gnus-uu-decode-postscript t]
2394               ["All MIME parts" gnus-summary-save-parts t])
2395              ("Cache"
2396               ["Enter article" gnus-cache-enter-article t]
2397               ["Remove article" gnus-cache-remove-article t])
2398              ["Translate" gnus-article-babel t]
2399              ["Select article buffer" gnus-summary-select-article-buffer t]
2400              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2401              ["Isearch article..." gnus-summary-isearch-article t]
2402              ["Beginning of the article" gnus-summary-beginning-of-article t]
2403              ["End of the article" gnus-summary-end-of-article t]
2404              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2405              ["Fetch referenced articles" gnus-summary-refer-references t]
2406              ["Fetch current thread" gnus-summary-refer-thread t]
2407              ["Fetch article with id..." gnus-summary-refer-article t]
2408              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2409              ["Redisplay" gnus-summary-show-article t]
2410              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2411       (easy-menu-define
2412         gnus-summary-article-menu gnus-summary-mode-map ""
2413         (cons "Article" innards))
2414
2415       (if (not (keymapp gnus-summary-article-menu))
2416           (easy-menu-define
2417             gnus-article-commands-menu gnus-article-mode-map ""
2418             (cons "Commands" innards))
2419         ;; in Emacs, don't share menu.
2420         (setq gnus-article-commands-menu
2421               (copy-keymap gnus-summary-article-menu))
2422         (define-key gnus-article-mode-map [menu-bar commands]
2423           (cons "Commands" gnus-article-commands-menu))))
2424
2425     (easy-menu-define
2426       gnus-summary-thread-menu gnus-summary-mode-map ""
2427       '("Threads"
2428         ["Find all messages in thread" gnus-summary-refer-thread t]
2429         ["Toggle threading" gnus-summary-toggle-threads t]
2430         ["Hide threads" gnus-summary-hide-all-threads t]
2431         ["Show threads" gnus-summary-show-all-threads t]
2432         ["Hide thread" gnus-summary-hide-thread t]
2433         ["Show thread" gnus-summary-show-thread t]
2434         ["Go to next thread" gnus-summary-next-thread t]
2435         ["Go to previous thread" gnus-summary-prev-thread t]
2436         ["Go down thread" gnus-summary-down-thread t]
2437         ["Go up thread" gnus-summary-up-thread t]
2438         ["Top of thread" gnus-summary-top-thread t]
2439         ["Mark thread as read" gnus-summary-kill-thread t]
2440         ["Lower thread score" gnus-summary-lower-thread t]
2441         ["Raise thread score" gnus-summary-raise-thread t]
2442         ["Rethread current" gnus-summary-rethread-current t]))
2443
2444     (easy-menu-define
2445       gnus-summary-post-menu gnus-summary-mode-map ""
2446       `("Post"
2447         ["Send a message (mail or news)" gnus-summary-post-news
2448          ,@(if (featurep 'xemacs) '(t)
2449              '(:help "Compose a new message (mail or news)"))]
2450         ["Followup" gnus-summary-followup
2451          ,@(if (featurep 'xemacs) '(t)
2452              '(:help "Post followup to this article"))]
2453         ["Followup and yank" gnus-summary-followup-with-original
2454          ,@(if (featurep 'xemacs) '(t)
2455              '(:help "Post followup to this article, quoting its contents"))]
2456         ["Supersede article" gnus-summary-supersede-article t]
2457         ["Cancel article" gnus-summary-cancel-article
2458          ,@(if (featurep 'xemacs) '(t)
2459              '(:help "Cancel an article you posted"))]
2460         ["Reply" gnus-summary-reply t]
2461         ["Reply and yank" gnus-summary-reply-with-original t]
2462         ["Wide reply" gnus-summary-wide-reply t]
2463         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2464          ,@(if (featurep 'xemacs) '(t)
2465              '(:help "Mail a reply, quoting this article"))]
2466         ["Very wide reply" gnus-summary-very-wide-reply t]
2467         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2468          ,@(if (featurep 'xemacs) '(t)
2469              '(:help "Mail a very wide reply, quoting this article"))]
2470         ["Mail forward" gnus-summary-mail-forward t]
2471         ["Post forward" gnus-summary-post-forward t]
2472         ["Digest and mail" gnus-uu-digest-mail-forward t]
2473         ["Digest and post" gnus-uu-digest-post-forward t]
2474         ["Resend message" gnus-summary-resend-message t]
2475         ["Resend message edit" gnus-summary-resend-message-edit t]
2476         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2477         ["Send a mail" gnus-summary-mail-other-window t]
2478         ["Create a local message" gnus-summary-news-other-window t]
2479         ["Uuencode and post" gnus-uu-post-news
2480          ,@(if (featurep 'xemacs) '(t)
2481              '(:help "Post a uuencoded article"))]
2482         ["Followup via news" gnus-summary-followup-to-mail t]
2483         ["Followup via news and yank"
2484          gnus-summary-followup-to-mail-with-original t]
2485         ;;("Draft"
2486         ;;["Send" gnus-summary-send-draft t]
2487         ;;["Send bounced" gnus-resend-bounced-mail t])
2488         ))
2489
2490     (cond
2491      ((not (keymapp gnus-summary-post-menu))
2492       (setq gnus-article-post-menu gnus-summary-post-menu))
2493      ((not gnus-article-post-menu)
2494       ;; Don't share post menu.
2495       (setq gnus-article-post-menu
2496             (copy-keymap gnus-summary-post-menu))))
2497     (define-key gnus-article-mode-map [menu-bar post]
2498       (cons "Post" gnus-article-post-menu))
2499
2500     (easy-menu-define
2501       gnus-summary-misc-menu gnus-summary-mode-map ""
2502       `("Gnus"
2503         ("Mark Read"
2504          ["Mark as read" gnus-summary-mark-as-read-forward t]
2505          ["Mark same subject and select"
2506           gnus-summary-kill-same-subject-and-select t]
2507          ["Mark same subject" gnus-summary-kill-same-subject t]
2508          ["Catchup" gnus-summary-catchup
2509           ,@(if (featurep 'xemacs) '(t)
2510               '(:help "Mark unread articles in this group as read"))]
2511          ["Catchup all" gnus-summary-catchup-all t]
2512          ["Catchup to here" gnus-summary-catchup-to-here t]
2513          ["Catchup from here" gnus-summary-catchup-from-here t]
2514          ["Catchup region" gnus-summary-mark-region-as-read
2515           (gnus-mark-active-p)]
2516          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2517         ("Mark Various"
2518          ["Tick" gnus-summary-tick-article-forward t]
2519          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2520          ["Remove marks" gnus-summary-clear-mark-forward t]
2521          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2522          ["Set bookmark" gnus-summary-set-bookmark t]
2523          ["Remove bookmark" gnus-summary-remove-bookmark t])
2524         ("Limit to"
2525          ["Marks..." gnus-summary-limit-to-marks t]
2526          ["Subject..." gnus-summary-limit-to-subject t]
2527          ["Author..." gnus-summary-limit-to-author t]
2528          ["Recipient..." gnus-summary-limit-to-recipient t]
2529          ["Age..." gnus-summary-limit-to-age t]
2530          ["Extra..." gnus-summary-limit-to-extra t]
2531          ["Score..." gnus-summary-limit-to-score t]
2532          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2533          ["Unread" gnus-summary-limit-to-unread t]
2534          ["Unseen" gnus-summary-limit-to-unseen t]
2535          ["Replied" gnus-summary-limit-to-replied t]
2536          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2537          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2538          ["Pop limit" gnus-summary-pop-limit t]
2539          ["Show dormant" gnus-summary-limit-include-dormant t]
2540          ["Hide childless dormant"
2541           gnus-summary-limit-exclude-childless-dormant t]
2542          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2543          ["Hide marked" gnus-summary-limit-exclude-marks t]
2544          ["Show expunged" gnus-summary-limit-include-expunged t])
2545         ("Process Mark"
2546          ["Set mark" gnus-summary-mark-as-processable t]
2547          ["Remove mark" gnus-summary-unmark-as-processable t]
2548          ["Remove all marks" gnus-summary-unmark-all-processable t]
2549          ["Invert marks" gnus-uu-invert-processable t]
2550          ["Mark above" gnus-uu-mark-over t]
2551          ["Mark series" gnus-uu-mark-series t]
2552          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2553          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2554          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2555          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2556          ["Mark all" gnus-uu-mark-all t]
2557          ["Mark buffer" gnus-uu-mark-buffer t]
2558          ["Mark sparse" gnus-uu-mark-sparse t]
2559          ["Mark thread" gnus-uu-mark-thread t]
2560          ["Unmark thread" gnus-uu-unmark-thread t]
2561          ("Process Mark Sets"
2562           ["Kill" gnus-summary-kill-process-mark t]
2563           ["Yank" gnus-summary-yank-process-mark
2564            gnus-newsgroup-process-stack]
2565           ["Save" gnus-summary-save-process-mark t]
2566           ["Run command on marked..." gnus-summary-universal-argument t]))
2567         ("Scroll article"
2568          ["Page forward" gnus-summary-next-page
2569           ,@(if (featurep 'xemacs) '(t)
2570               '(:help "Show next page of article"))]
2571          ["Page backward" gnus-summary-prev-page
2572           ,@(if (featurep 'xemacs) '(t)
2573               '(:help "Show previous page of article"))]
2574          ["Line forward" gnus-summary-scroll-up t])
2575         ("Move"
2576          ["Next unread article" gnus-summary-next-unread-article t]
2577          ["Previous unread article" gnus-summary-prev-unread-article t]
2578          ["Next article" gnus-summary-next-article t]
2579          ["Previous article" gnus-summary-prev-article t]
2580          ["Next unread subject" gnus-summary-next-unread-subject t]
2581          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2582          ["Next article same subject" gnus-summary-next-same-subject t]
2583          ["Previous article same subject" gnus-summary-prev-same-subject t]
2584          ["First unread article" gnus-summary-first-unread-article t]
2585          ["Best unread article" gnus-summary-best-unread-article t]
2586          ["Go to subject number..." gnus-summary-goto-subject t]
2587          ["Go to article number..." gnus-summary-goto-article t]
2588          ["Go to the last article" gnus-summary-goto-last-article t]
2589          ["Pop article off history" gnus-summary-pop-article t])
2590         ("Sort"
2591          ["Sort by number" gnus-summary-sort-by-number t]
2592          ["Sort by author" gnus-summary-sort-by-author t]
2593          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2594          ["Sort by subject" gnus-summary-sort-by-subject t]
2595          ["Sort by date" gnus-summary-sort-by-date t]
2596          ["Sort by score" gnus-summary-sort-by-score t]
2597          ["Sort by lines" gnus-summary-sort-by-lines t]
2598          ["Sort by characters" gnus-summary-sort-by-chars t]
2599          ["Randomize" gnus-summary-sort-by-random t]
2600          ["Original sort" gnus-summary-sort-by-original t])
2601         ("Help"
2602          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2603          ["Describe group" gnus-summary-describe-group t]
2604          ["Fetch charter" gnus-group-fetch-charter
2605           ,@(if (featurep 'xemacs) nil
2606               '(:help "Display the charter of the current group"))]
2607          ["Fetch control message" gnus-group-fetch-control
2608           ,@(if (featurep 'xemacs) nil
2609               '(:help "Display the archived control message for the current group"))]
2610          ["Read manual" gnus-info-find-node t])
2611         ("Modes"
2612          ["Pick and read" gnus-pick-mode t]
2613          ["Binary" gnus-binary-mode t])
2614         ("Regeneration"
2615          ["Regenerate" gnus-summary-prepare t]
2616          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2617          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2618          ["Toggle threading" gnus-summary-toggle-threads t])
2619         ["See old articles" gnus-summary-insert-old-articles t]
2620         ["See new articles" gnus-summary-insert-new-articles t]
2621         ["Filter articles..." gnus-summary-execute-command t]
2622         ["Run command on articles..." gnus-summary-universal-argument t]
2623         ["Search articles forward..." gnus-summary-search-article-forward t]
2624         ["Search articles backward..." gnus-summary-search-article-backward t]
2625         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2626         ["Expand window" gnus-summary-expand-window t]
2627         ["Expire expirable articles" gnus-summary-expire-articles
2628          (gnus-check-backend-function
2629           'request-expire-articles gnus-newsgroup-name)]
2630         ["Edit local kill file" gnus-summary-edit-local-kill t]
2631         ["Edit main kill file" gnus-summary-edit-global-kill t]
2632         ["Edit group parameters" gnus-summary-edit-parameters t]
2633         ["Customize group parameters" gnus-summary-customize-parameters t]
2634         ["Send a bug report" gnus-bug t]
2635         ("Exit"
2636          ["Catchup and exit" gnus-summary-catchup-and-exit
2637           ,@(if (featurep 'xemacs) '(t)
2638               '(:help "Mark unread articles in this group as read, then exit"))]
2639          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2640          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2641          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2642          ["Exit group" gnus-summary-exit
2643           ,@(if (featurep 'xemacs) '(t)
2644               '(:help "Exit current group, return to group selection mode"))]
2645          ["Exit group without updating" gnus-summary-exit-no-update t]
2646          ["Exit and goto next group" gnus-summary-next-group t]
2647          ["Exit and goto prev group" gnus-summary-prev-group t]
2648          ["Reselect group" gnus-summary-reselect-current-group t]
2649          ["Rescan group" gnus-summary-rescan-group t]
2650          ["Update dribble" gnus-summary-save-newsrc t])))
2651
2652     (gnus-run-hooks 'gnus-summary-menu-hook)))
2653
2654 (defvar gnus-summary-tool-bar-map nil)
2655
2656 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2657 ;; affect _new_ message buffers.  We might add a function that walks thru all
2658 ;; summary-mode buffers and force the update.
2659 (defun gnus-summary-tool-bar-update (&optional symbol value)
2660   "Update summary mode toolbar.
2661 Setter function for custom variables."
2662   (setq-default gnus-summary-tool-bar-map nil)
2663   (when symbol
2664     ;; When used as ":set" function:
2665     (set-default symbol value))
2666   (when (gnus-buffer-live-p gnus-summary-buffer)
2667     (with-current-buffer gnus-summary-buffer
2668       (gnus-summary-make-tool-bar))))
2669
2670 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2671                                      'gnus-summary-tool-bar-gnome
2672                                    'gnus-summary-tool-bar-retro)
2673   "Specifies the Gnus summary tool bar.
2674
2675 It can be either a list or a symbol refering to a list.  See
2676 `gmm-tool-bar-from-list' for the format of the list.  The
2677 default key map is `gnus-summary-mode-map'.
2678
2679 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2680 `gnus-summary-tool-bar-retro'."
2681   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2682                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2683                  (repeat :tag "User defined list" gmm-tool-bar-item)
2684                  (symbol))
2685   :version "23.0" ;; No Gnus
2686   :initialize 'custom-initialize-default
2687   :set 'gnus-summary-tool-bar-update
2688   :group 'gnus-summary)
2689
2690 (defcustom gnus-summary-tool-bar-gnome
2691   '((gnus-summary-post-news "mail/compose" nil)
2692     (gnus-summary-insert-new-articles "mail/inbox" nil
2693                                       :visible (or (not gnus-agent)
2694                                                    gnus-plugged))
2695     (gnus-summary-reply-with-original "mail/reply")
2696     (gnus-summary-reply "mail/reply" nil :visible nil)
2697     (gnus-summary-followup-with-original "mail/reply-all")
2698     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2699     (gnus-summary-mail-forward "mail/forward")
2700     (gnus-summary-save-article "mail/save")
2701     (gnus-summary-search-article-forward "search" nil :visible nil)
2702     (gnus-summary-print-article "print")
2703     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2704     ;; Some new commands that may need more suitable icons:
2705     (gnus-summary-save-newsrc "save" nil :visible nil)
2706     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2707     (gnus-summary-prev-article "left-arrow")
2708     (gnus-summary-next-article "right-arrow")
2709     (gnus-summary-next-page "next-page")
2710     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2711     ;;
2712     ;; Maybe some sort-by-... could be added:
2713     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2714     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2715     (gnus-summary-mark-as-expirable
2716      "delete" nil
2717      :visible (gnus-check-backend-function 'request-expire-articles
2718                                            gnus-newsgroup-name))
2719     (gnus-summary-mark-as-spam
2720      "mail/spam" t
2721      :visible (and (fboundp 'spam-group-ham-contents-p)
2722                    (spam-group-ham-contents-p gnus-newsgroup-name))
2723      :help "Mark as spam")
2724     (gnus-summary-mark-as-read-forward
2725      "mail/not-spam" nil
2726      :visible (and (fboundp 'spam-group-spam-contents-p)
2727                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2728     ;;
2729     (gnus-summary-exit "exit")
2730     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2731     (gnus-info-find-node "help"))
2732   "List of functions for the summary tool bar (GNOME style).
2733
2734 See `gmm-tool-bar-from-list' for the format of the list."
2735   :type '(repeat gmm-tool-bar-item)
2736   :version "23.0" ;; No Gnus
2737   :initialize 'custom-initialize-default
2738   :set 'gnus-summary-tool-bar-update
2739   :group 'gnus-summary)
2740
2741 (defcustom gnus-summary-tool-bar-retro
2742   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2743     (gnus-summary-next-unread-article "gnus/next-ur")
2744     (gnus-summary-post-news "gnus/post")
2745     (gnus-summary-followup-with-original "gnus/fuwo")
2746     (gnus-summary-followup "gnus/followup")
2747     (gnus-summary-reply-with-original "gnus/reply-wo")
2748     (gnus-summary-reply "gnus/reply")
2749     (gnus-summary-caesar-message "gnus/rot13")
2750     (gnus-uu-decode-uu "gnus/uu-decode")
2751     (gnus-summary-save-article-file "gnus/save-aif")
2752     (gnus-summary-save-article "gnus/save-art")
2753     (gnus-uu-post-news "gnus/uu-post")
2754     (gnus-summary-catchup "gnus/catchup")
2755     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2756     (gnus-summary-exit "gnus/exit-summ")
2757     ;; Some new command that may need more suitable icons:
2758     (gnus-summary-print-article "gnus/print" nil :visible nil)
2759     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2760     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2761     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2762     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2763     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2764     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2765     ;;
2766     (gnus-info-find-node "gnus/help" nil :visible nil))
2767   "List of functions for the summary tool bar (retro look).
2768
2769 See `gmm-tool-bar-from-list' for the format of the list."
2770   :type '(repeat gmm-tool-bar-item)
2771   :version "23.0" ;; No Gnus
2772   :initialize 'custom-initialize-default
2773   :set 'gnus-summary-tool-bar-update
2774   :group 'gnus-summary)
2775
2776 (defcustom gnus-summary-tool-bar-zap-list t
2777   "List of icon items from the global tool bar.
2778 These items are not displayed in the Gnus summary mode tool bar.
2779
2780 See `gmm-tool-bar-from-list' for the format of the list."
2781   :type 'gmm-tool-bar-zap-list
2782   :version "23.0" ;; No Gnus
2783   :initialize 'custom-initialize-default
2784   :set 'gnus-summary-tool-bar-update
2785   :group 'gnus-summary)
2786
2787 (defvar image-load-path)
2788
2789 (defun gnus-summary-make-tool-bar (&optional force)
2790   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2791 When FORCE, rebuild the tool bar."
2792   (when (and (not (featurep 'xemacs))
2793              (boundp 'tool-bar-mode)
2794              tool-bar-mode
2795              (or (not gnus-summary-tool-bar-map) force))
2796     (let* ((load-path
2797             (gmm-image-load-path-for-library "gnus"
2798                                              "mail/save.xpm"
2799                                              nil t))
2800            (image-load-path (cons (car load-path)
2801                                   (when (boundp 'image-load-path)
2802                                     image-load-path)))
2803            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2804                                         gnus-summary-tool-bar-zap-list
2805                                         'gnus-summary-mode-map)))
2806       (when map
2807         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2808         ;; uses it's value.
2809         (setq gnus-summary-tool-bar-map map))))
2810   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2811
2812 (defun gnus-score-set-default (var value)
2813   "A version of set that updates the GNU Emacs menu-bar."
2814   (set var value)
2815   ;; It is the message that forces the active status to be updated.
2816   (message ""))
2817
2818 (defun gnus-make-score-map (type)
2819   "Make a summary score map of type TYPE."
2820   (if t
2821       nil
2822     (let ((headers '(("author" "from" string)
2823                      ("subject" "subject" string)
2824                      ("article body" "body" string)
2825                      ("article head" "head" string)
2826                      ("xref" "xref" string)
2827                      ("extra header" "extra" string)
2828                      ("lines" "lines" number)
2829                      ("followups to author" "followup" string)))
2830           (types '((number ("less than" <)
2831                            ("greater than" >)
2832                            ("equal" =))
2833                    (string ("substring" s)
2834                            ("exact string" e)
2835                            ("fuzzy string" f)
2836                            ("regexp" r))))
2837           (perms '(("temporary" (current-time-string))
2838                    ("permanent" nil)
2839                    ("immediate" now)))
2840           header)
2841       (list
2842        (apply
2843         'nconc
2844         (list
2845          (if (eq type 'lower)
2846              "Lower score"
2847            "Increase score"))
2848         (let (outh)
2849           (while headers
2850             (setq header (car headers))
2851             (setq outh
2852                   (cons
2853                    (apply
2854                     'nconc
2855                     (list (car header))
2856                     (let ((ts (cdr (assoc (nth 2 header) types)))
2857                           outt)
2858                       (while ts
2859                         (setq outt
2860                               (cons
2861                                (apply
2862                                 'nconc
2863                                 (list (caar ts))
2864                                 (let ((ps perms)
2865                                       outp)
2866                                   (while ps
2867                                     (setq outp
2868                                           (cons
2869                                            (vector
2870                                             (caar ps)
2871                                             (list
2872                                              'gnus-summary-score-entry
2873                                              (nth 1 header)
2874                                              (if (or (string= (nth 1 header)
2875                                                               "head")
2876                                                      (string= (nth 1 header)
2877                                                               "body"))
2878                                                  ""
2879                                                (list 'gnus-summary-header
2880                                                      (nth 1 header)))
2881                                              (list 'quote (nth 1 (car ts)))
2882                                              (list 'gnus-score-delta-default
2883                                                    nil)
2884                                              (nth 1 (car ps))
2885                                              t)
2886                                             t)
2887                                            outp))
2888                                     (setq ps (cdr ps)))
2889                                   (list (nreverse outp))))
2890                                outt))
2891                         (setq ts (cdr ts)))
2892                       (list (nreverse outt))))
2893                    outh))
2894             (setq headers (cdr headers)))
2895           (list (nreverse outh))))))))
2896
2897 \f
2898
2899 (defun gnus-summary-mode (&optional group)
2900   "Major mode for reading articles.
2901
2902 All normal editing commands are switched off.
2903 \\<gnus-summary-mode-map>
2904 Each line in this buffer represents one article.  To read an
2905 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2906 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2907 respectively.
2908
2909 You can also post articles and send mail from this buffer.  To
2910 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2911 of an article, type `\\[gnus-summary-reply]'.
2912
2913 There are approx. one gazillion commands you can execute in this
2914 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2915
2916 The following commands are available:
2917
2918 \\{gnus-summary-mode-map}"
2919   (interactive)
2920   (kill-all-local-variables)
2921   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2922     (gnus-summary-make-local-variables))
2923   (gnus-summary-make-local-variables)
2924   (setq gnus-newsgroup-name group)
2925   (when (gnus-visual-p 'summary-menu 'menu)
2926     (gnus-summary-make-menu-bar)
2927     (gnus-summary-make-tool-bar))
2928   (gnus-make-thread-indent-array)
2929   (gnus-simplify-mode-line)
2930   (setq major-mode 'gnus-summary-mode)
2931   (setq mode-name "Summary")
2932   (make-local-variable 'minor-mode-alist)
2933   (use-local-map gnus-summary-mode-map)
2934   (buffer-disable-undo)
2935   (setq buffer-read-only t              ;Disable modification
2936         show-trailing-whitespace nil)
2937   (setq truncate-lines t)
2938   (setq selective-display t)
2939   (setq selective-display-ellipses t)   ;Display `...'
2940   (gnus-summary-set-display-table)
2941   (gnus-set-default-directory)
2942   (make-local-variable 'gnus-summary-line-format)
2943   (make-local-variable 'gnus-summary-line-format-spec)
2944   (make-local-variable 'gnus-summary-dummy-line-format)
2945   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2946   (make-local-variable 'gnus-summary-mark-positions)
2947   (gnus-make-local-hook 'pre-command-hook)
2948   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2949   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
2950   (turn-on-gnus-mailing-list-mode)
2951   (mm-enable-multibyte)
2952   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2953   (gnus-update-summary-mark-positions))
2954
2955 (defun gnus-summary-make-local-variables ()
2956   "Make all the local summary buffer variables."
2957   (let (global)
2958     (dolist (local gnus-summary-local-variables)
2959       (if (consp local)
2960           (progn
2961             (if (eq (cdr local) 'global)
2962                 ;; Copy the global value of the variable.
2963                 (setq global (symbol-value (car local)))
2964               ;; Use the value from the list.
2965               (setq global (eval (cdr local))))
2966             (set (make-local-variable (car local)) global))
2967         ;; Simple nil-valued local variable.
2968         (set (make-local-variable local) nil)))))
2969
2970 (defun gnus-summary-clear-local-variables ()
2971   (let ((locals gnus-summary-local-variables))
2972     (while locals
2973       (if (consp (car locals))
2974           (and (symbolp (caar locals))
2975                (set (caar locals) nil))
2976         (and (symbolp (car locals))
2977              (set (car locals) nil)))
2978       (setq locals (cdr locals)))))
2979
2980 ;; Summary data functions.
2981
2982 (defmacro gnus-data-number (data)
2983   `(car ,data))
2984
2985 (defmacro gnus-data-set-number (data number)
2986   `(setcar ,data ,number))
2987
2988 (defmacro gnus-data-mark (data)
2989   `(nth 1 ,data))
2990
2991 (defmacro gnus-data-set-mark (data mark)
2992   `(setcar (nthcdr 1 ,data) ,mark))
2993
2994 (defmacro gnus-data-pos (data)
2995   `(nth 2 ,data))
2996
2997 (defmacro gnus-data-set-pos (data pos)
2998   `(setcar (nthcdr 2 ,data) ,pos))
2999
3000 (defmacro gnus-data-header (data)
3001   `(nth 3 ,data))
3002
3003 (defmacro gnus-data-set-header (data header)
3004   `(setf (nth 3 ,data) ,header))
3005
3006 (defmacro gnus-data-level (data)
3007   `(nth 4 ,data))
3008
3009 (defmacro gnus-data-unread-p (data)
3010   `(= (nth 1 ,data) gnus-unread-mark))
3011
3012 (defmacro gnus-data-read-p (data)
3013   `(/= (nth 1 ,data) gnus-unread-mark))
3014
3015 (defmacro gnus-data-pseudo-p (data)
3016   `(consp (nth 3 ,data)))
3017
3018 (defmacro gnus-data-find (number)
3019   `(assq ,number gnus-newsgroup-data))
3020
3021 (defmacro gnus-data-find-list (number &optional data)
3022   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3023      (memq (assq ,number bdata)
3024            bdata)))
3025
3026 (defmacro gnus-data-make (number mark pos header level)
3027   `(list ,number ,mark ,pos ,header ,level))
3028
3029 (defun gnus-data-enter (after-article number mark pos header level offset)
3030   (let ((data (gnus-data-find-list after-article)))
3031     (unless data
3032       (error "No such article: %d" after-article))
3033     (setcdr data (cons (gnus-data-make number mark pos header level)
3034                        (cdr data)))
3035     (setq gnus-newsgroup-data-reverse nil)
3036     (gnus-data-update-list (cddr data) offset)))
3037
3038 (defun gnus-data-enter-list (after-article list &optional offset)
3039   (when list
3040     (let ((data (and after-article (gnus-data-find-list after-article)))
3041           (ilist list))
3042       (if (not (or data
3043                    after-article))
3044           (let ((odata gnus-newsgroup-data))
3045             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3046             (when offset
3047               (gnus-data-update-list odata offset)))
3048         ;; Find the last element in the list to be spliced into the main
3049         ;; list.
3050         (setq list (last list))
3051         (if (not data)
3052             (progn
3053               (setcdr list gnus-newsgroup-data)
3054               (setq gnus-newsgroup-data ilist)
3055               (when offset
3056                 (gnus-data-update-list (cdr list) offset)))
3057           (setcdr list (cdr data))
3058           (setcdr data ilist)
3059           (when offset
3060             (gnus-data-update-list (cdr list) offset))))
3061       (setq gnus-newsgroup-data-reverse nil))))
3062
3063 (defun gnus-data-remove (article &optional offset)
3064   (let ((data gnus-newsgroup-data))
3065     (if (= (gnus-data-number (car data)) article)
3066         (progn
3067           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3068                 gnus-newsgroup-data-reverse nil)
3069           (when offset
3070             (gnus-data-update-list gnus-newsgroup-data offset)))
3071       (while (cdr data)
3072         (when (= (gnus-data-number (cadr data)) article)
3073           (setcdr data (cddr data))
3074           (when offset
3075             (gnus-data-update-list (cdr data) offset))
3076           (setq data nil
3077                 gnus-newsgroup-data-reverse nil))
3078         (setq data (cdr data))))))
3079
3080 (defmacro gnus-data-list (backward)
3081   `(if ,backward
3082        (or gnus-newsgroup-data-reverse
3083            (setq gnus-newsgroup-data-reverse
3084                  (reverse gnus-newsgroup-data)))
3085      gnus-newsgroup-data))
3086
3087 (defun gnus-data-update-list (data offset)
3088   "Add OFFSET to the POS of all data entries in DATA."
3089   (setq gnus-newsgroup-data-reverse nil)
3090   (while data
3091     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3092     (setq data (cdr data))))
3093
3094 (defun gnus-summary-article-pseudo-p (article)
3095   "Say whether this article is a pseudo article or not."
3096   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3097
3098 (defmacro gnus-summary-article-sparse-p (article)
3099   "Say whether this article is a sparse article or not."
3100   `(memq ,article gnus-newsgroup-sparse))
3101
3102 (defmacro gnus-summary-article-ancient-p (article)
3103   "Say whether this article is a sparse article or not."
3104   `(memq ,article gnus-newsgroup-ancient))
3105
3106 (defun gnus-article-parent-p (number)
3107   "Say whether this article is a parent or not."
3108   (let ((data (gnus-data-find-list number)))
3109     (and (cdr data)              ; There has to be an article after...
3110          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3111             (gnus-data-level (nth 1 data))))))
3112
3113 (defun gnus-article-children (number)
3114   "Return a list of all children to NUMBER."
3115   (let* ((data (gnus-data-find-list number))
3116          (level (gnus-data-level (car data)))
3117          children)
3118     (setq data (cdr data))
3119     (while (and data
3120                 (= (gnus-data-level (car data)) (1+ level)))
3121       (push (gnus-data-number (car data)) children)
3122       (setq data (cdr data)))
3123     children))
3124
3125 (defmacro gnus-summary-skip-intangible ()
3126   "If the current article is intangible, then jump to a different article."
3127   '(let ((to (get-text-property (point) 'gnus-intangible)))
3128      (and to (gnus-summary-goto-subject to))))
3129
3130 (defmacro gnus-summary-article-intangible-p ()
3131   "Say whether this article is intangible or not."
3132   '(get-text-property (point) 'gnus-intangible))
3133
3134 (defun gnus-article-read-p (article)
3135   "Say whether ARTICLE is read or not."
3136   (not (or (memq article gnus-newsgroup-marked)
3137            (memq article gnus-newsgroup-spam-marked)
3138            (memq article gnus-newsgroup-unreads)
3139            (memq article gnus-newsgroup-unselected)
3140            (memq article gnus-newsgroup-dormant))))
3141
3142 ;; Some summary mode macros.
3143
3144 (defmacro gnus-summary-article-number ()
3145   "The article number of the article on the current line.
3146 If there isn't an article number here, then we return the current
3147 article number."
3148   '(progn
3149      (gnus-summary-skip-intangible)
3150      (or (get-text-property (point) 'gnus-number)
3151          (gnus-summary-last-subject))))
3152
3153 (defmacro gnus-summary-article-header (&optional number)
3154   "Return the header of article NUMBER."
3155   `(gnus-data-header (gnus-data-find
3156                       ,(or number '(gnus-summary-article-number)))))
3157
3158 (defmacro gnus-summary-thread-level (&optional number)
3159   "Return the level of thread that starts with article NUMBER."
3160   `(if (and (eq gnus-summary-make-false-root 'dummy)
3161             (get-text-property (point) 'gnus-intangible))
3162        0
3163      (gnus-data-level (gnus-data-find
3164                        ,(or number '(gnus-summary-article-number))))))
3165
3166 (defmacro gnus-summary-article-mark (&optional number)
3167   "Return the mark of article NUMBER."
3168   `(gnus-data-mark (gnus-data-find
3169                     ,(or number '(gnus-summary-article-number)))))
3170
3171 (defmacro gnus-summary-article-pos (&optional number)
3172   "Return the position of the line of article NUMBER."
3173   `(gnus-data-pos (gnus-data-find
3174                    ,(or number '(gnus-summary-article-number)))))
3175
3176 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3177 (defmacro gnus-summary-article-subject (&optional number)
3178   "Return current subject string or nil if nothing."
3179   `(let ((headers
3180           ,(if number
3181                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3182              '(gnus-data-header (assq (gnus-summary-article-number)
3183                                       gnus-newsgroup-data)))))
3184      (and headers
3185           (vectorp headers)
3186           (mail-header-subject headers))))
3187
3188 (defmacro gnus-summary-article-score (&optional number)
3189   "Return current article score."
3190   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3191                   gnus-newsgroup-scored))
3192        gnus-summary-default-score 0))
3193
3194 (defun gnus-summary-article-children (&optional number)
3195   "Return a list of article numbers that are children of article NUMBER."
3196   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3197          (level (gnus-data-level (car data)))
3198          l children)
3199     (while (and (setq data (cdr data))
3200                 (> (setq l (gnus-data-level (car data))) level))
3201       (and (= (1+ level) l)
3202            (push (gnus-data-number (car data))
3203                  children)))
3204     (nreverse children)))
3205
3206 (defun gnus-summary-article-parent (&optional number)
3207   "Return the article number of the parent of article NUMBER."
3208   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3209                                     (gnus-data-list t)))
3210          (level (gnus-data-level (car data))))
3211     (if (zerop level)
3212         ()                              ; This is a root.
3213       ;; We search until we find an article with a level less than
3214       ;; this one.  That function has to be the parent.
3215       (while (and (setq data (cdr data))
3216                   (not (< (gnus-data-level (car data)) level))))
3217       (and data (gnus-data-number (car data))))))
3218
3219 (defun gnus-unread-mark-p (mark)
3220   "Say whether MARK is the unread mark."
3221   (= mark gnus-unread-mark))
3222
3223 (defun gnus-read-mark-p (mark)
3224   "Say whether MARK is one of the marks that mark as read.
3225 This is all marks except unread, ticked, dormant, and expirable."
3226   (not (or (= mark gnus-unread-mark)
3227            (= mark gnus-ticked-mark)
3228            (= mark gnus-spam-mark)
3229            (= mark gnus-dormant-mark)
3230            (= mark gnus-expirable-mark))))
3231
3232 (defmacro gnus-article-mark (number)
3233   "Return the MARK of article NUMBER.
3234 This macro should only be used when computing the mark the \"first\"
3235 time; i.e., when generating the summary lines.  After that,
3236 `gnus-summary-article-mark' should be used to examine the
3237 marks of articles."
3238   `(cond
3239     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3240     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3241     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3242     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3243     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3244     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3245     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3246     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3247            gnus-ancient-mark))))
3248
3249 ;; Saving hidden threads.
3250
3251 (defmacro gnus-save-hidden-threads (&rest forms)
3252   "Save hidden threads, eval FORMS, and restore the hidden threads."
3253   (let ((config (make-symbol "config")))
3254     `(let ((,config (gnus-hidden-threads-configuration)))
3255        (unwind-protect
3256            (save-excursion
3257              ,@forms)
3258          (gnus-restore-hidden-threads-configuration ,config)))))
3259 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3260 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3261
3262 (defun gnus-data-compute-positions ()
3263   "Compute the positions of all articles."
3264   (setq gnus-newsgroup-data-reverse nil)
3265   (let ((data gnus-newsgroup-data))
3266     (save-excursion
3267       (gnus-save-hidden-threads
3268         (gnus-summary-show-all-threads)
3269         (goto-char (point-min))
3270         (while data
3271           (while (get-text-property (point) 'gnus-intangible)
3272             (forward-line 1))
3273           (gnus-data-set-pos (car data) (+ (point) 3))
3274           (setq data (cdr data))
3275           (forward-line 1))))))
3276
3277 (defun gnus-hidden-threads-configuration ()
3278   "Return the current hidden threads configuration."
3279   (save-excursion
3280     (let (config)
3281       (goto-char (point-min))
3282       (while (search-forward "\r" nil t)
3283         (push (1- (point)) config))
3284       config)))
3285
3286 (defun gnus-restore-hidden-threads-configuration (config)
3287   "Restore hidden threads configuration from CONFIG."
3288   (save-excursion
3289     (let (point buffer-read-only)
3290       (while (setq point (pop config))
3291         (when (and (< point (point-max))
3292                    (goto-char point)
3293                    (eq (char-after) ?\n))
3294           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3295
3296 ;; Various summary mode internalish functions.
3297
3298 (defun gnus-mouse-pick-article (e)
3299   (interactive "e")
3300   (mouse-set-point e)
3301   (gnus-summary-next-page nil t))
3302
3303 (defun gnus-summary-set-display-table ()
3304   "Change the display table.
3305 Odd characters have a tendency to mess
3306 up nicely formatted displays - we make all possible glyphs
3307 display only a single character."
3308
3309   ;; We start from the standard display table, if any.
3310   (let ((table (or (copy-sequence standard-display-table)
3311                    (make-display-table)))
3312         (i 32))
3313     ;; Nix out all the control chars...
3314     (while (>= (setq i (1- i)) 0)
3315       (aset table i [??]))
3316    ;; ... but not newline and cr, of course.  (cr is necessary for the
3317     ;; selective display).
3318     (aset table ?\n nil)
3319     (aset table ?\r nil)
3320     ;; We keep TAB as well.
3321     (aset table ?\t nil)
3322     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3323     ;; Emacs 23 (unicode), that are not set already.
3324     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3325                  160
3326                256)))
3327       (while (>= (setq i (1- i)) 127)
3328         ;; Only modify if the entry is nil.
3329         (unless (aref table i)
3330           (aset table i [??]))))
3331     (setq buffer-display-table table)))
3332
3333 (defun gnus-summary-set-article-display-arrow (pos)
3334   "Update the overlay arrow to point to line at position POS."
3335   (when (and gnus-summary-display-arrow
3336              (boundp 'overlay-arrow-position)
3337              (boundp 'overlay-arrow-string))
3338     (save-excursion
3339       (goto-char pos)
3340       (beginning-of-line)
3341       (unless overlay-arrow-position
3342         (setq overlay-arrow-position (make-marker)))
3343       (setq overlay-arrow-string "=>"
3344             overlay-arrow-position (set-marker overlay-arrow-position
3345                                                (point)
3346                                                (current-buffer))))))
3347
3348 (defun gnus-summary-setup-buffer (group)
3349   "Initialize summary buffer."
3350   (let ((buffer (gnus-summary-buffer-name group))
3351         (dead-name (concat "*Dead Summary "
3352                            (gnus-group-decoded-name group) "*")))
3353     ;; If a dead summary buffer exists, we kill it.
3354     (when (gnus-buffer-live-p dead-name)
3355       (gnus-kill-buffer dead-name))
3356     (if (get-buffer buffer)
3357         (progn
3358           (set-buffer buffer)
3359           (setq gnus-summary-buffer (current-buffer))
3360           (not gnus-newsgroup-prepared))
3361       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3362       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3363       (gnus-summary-mode group)
3364       (when gnus-carpal
3365         (gnus-carpal-setup-buffer 'summary))
3366       (when (gnus-group-quit-config group)
3367         (set (make-local-variable 'gnus-single-article-buffer) nil))
3368       (make-local-variable 'gnus-article-buffer)
3369       (make-local-variable 'gnus-article-current)
3370       (make-local-variable 'gnus-original-article-buffer)
3371       (setq gnus-newsgroup-name group)
3372       ;; Set any local variables in the group parameters.
3373       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3374       t)))
3375
3376 (defun gnus-set-global-variables ()
3377   "Set the global equivalents of the buffer-local variables.
3378 They are set to the latest values they had.  These reflect the summary
3379 buffer that was in action when the last article was fetched."
3380   (when (eq major-mode 'gnus-summary-mode)
3381     (setq gnus-summary-buffer (current-buffer))
3382     (let ((name gnus-newsgroup-name)
3383           (marked gnus-newsgroup-marked)
3384           (spam gnus-newsgroup-spam-marked)
3385           (unread gnus-newsgroup-unreads)
3386           (headers gnus-current-headers)
3387           (data gnus-newsgroup-data)
3388           (summary gnus-summary-buffer)
3389           (article-buffer gnus-article-buffer)
3390           (original gnus-original-article-buffer)
3391           (gac gnus-article-current)
3392           (reffed gnus-reffed-article-number)
3393           (score-file gnus-current-score-file)
3394           (default-charset gnus-newsgroup-charset)
3395           vlist)
3396       (let ((locals gnus-newsgroup-variables))
3397         (while locals
3398           (if (consp (car locals))
3399               (push (eval (caar locals)) vlist)
3400             (push (eval (car locals)) vlist))
3401           (setq locals (cdr locals)))
3402         (setq vlist (nreverse vlist)))
3403       (with-current-buffer gnus-group-buffer
3404         (setq gnus-newsgroup-name name
3405               gnus-newsgroup-marked marked
3406               gnus-newsgroup-spam-marked spam
3407               gnus-newsgroup-unreads unread
3408               gnus-current-headers headers
3409               gnus-newsgroup-data data
3410               gnus-article-current gac
3411               gnus-summary-buffer summary
3412               gnus-article-buffer article-buffer
3413               gnus-original-article-buffer original
3414               gnus-reffed-article-number reffed
3415               gnus-current-score-file score-file
3416               gnus-newsgroup-charset default-charset)
3417         (let ((locals gnus-newsgroup-variables))
3418           (while locals
3419             (if (consp (car locals))
3420                 (set (caar locals) (pop vlist))
3421               (set (car locals) (pop vlist)))
3422             (setq locals (cdr locals))))
3423         ;; The article buffer also has local variables.
3424         (when (gnus-buffer-live-p gnus-article-buffer)
3425           (set-buffer gnus-article-buffer)
3426           (setq gnus-summary-buffer summary))))))
3427
3428 (defun gnus-summary-article-unread-p (article)
3429   "Say whether ARTICLE is unread or not."
3430   (memq article gnus-newsgroup-unreads))
3431
3432 (defun gnus-summary-first-article-p (&optional article)
3433   "Return whether ARTICLE is the first article in the buffer."
3434   (if (not (setq article (or article (gnus-summary-article-number))))
3435       nil
3436     (eq article (caar gnus-newsgroup-data))))
3437
3438 (defun gnus-summary-last-article-p (&optional article)
3439   "Return whether ARTICLE is the last article in the buffer."
3440   (if (not (setq article (or article (gnus-summary-article-number))))
3441       ;; All non-existent numbers are the last article.  :-)
3442       t
3443     (not (cdr (gnus-data-find-list article)))))
3444
3445 (defun gnus-make-thread-indent-array ()
3446   (let ((n 200))
3447     (unless (and gnus-thread-indent-array
3448                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3449       (setq gnus-thread-indent-array (make-vector 201 "")
3450             gnus-thread-indent-array-level gnus-thread-indent-level)
3451       (while (>= n 0)
3452         (aset gnus-thread-indent-array n
3453               (make-string (* n gnus-thread-indent-level) ? ))
3454         (setq n (1- n))))))
3455
3456 (defun gnus-update-summary-mark-positions ()
3457   "Compute where the summary marks are to go."
3458   (save-excursion
3459     (when (gnus-buffer-exists-p gnus-summary-buffer)
3460       (set-buffer gnus-summary-buffer))
3461     (let ((spec gnus-summary-line-format-spec)
3462           pos)
3463       (save-excursion
3464         (gnus-set-work-buffer)
3465         (let ((gnus-tmp-unread ?Z)
3466               (gnus-replied-mark ?Z)
3467               (gnus-score-below-mark ?Z)
3468               (gnus-score-over-mark ?Z)
3469               (gnus-undownloaded-mark ?Z)
3470               (gnus-summary-line-format-spec spec)
3471               (gnus-newsgroup-downloadable '(0))
3472               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3473               case-fold-search ignores)
3474           ;; Here, all marks are bound to Z.
3475           (gnus-summary-insert-line header
3476                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3477           (goto-char (point-min))
3478           ;; Memorize the positions of the same characters as dummy marks.
3479           (while (re-search-forward "[A-D]" nil t)
3480             (push (point) ignores))
3481           (erase-buffer)
3482           ;; We use A-D as dummy marks in order to know column positions
3483           ;; where marks should be inserted.
3484           (setq gnus-tmp-unread ?A
3485                 gnus-replied-mark ?B
3486                 gnus-score-below-mark ?C
3487                 gnus-score-over-mark ?C
3488                 gnus-undownloaded-mark ?D)
3489           (gnus-summary-insert-line header
3490                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3491           ;; Ignore characters which aren't dummy marks.
3492           (dolist (p ignores)
3493             (delete-region (goto-char (1- p)) p)
3494             (insert ?Z))
3495           (goto-char (point-min))
3496           (setq pos (list (cons 'unread
3497                                 (and (search-forward "A" nil t)
3498                                      (- (point) (point-min) 1)))))
3499           (goto-char (point-min))
3500           (push (cons 'replied (and (search-forward "B" nil t)
3501                                     (- (point) (point-min) 1)))
3502                 pos)
3503           (goto-char (point-min))
3504           (push (cons 'score (and (search-forward "C" nil t)
3505                                   (- (point) (point-min) 1)))
3506                 pos)
3507           (goto-char (point-min))
3508           (push (cons 'download (and (search-forward "D" nil t)
3509                                      (- (point) (point-min) 1)))
3510                 pos)))
3511       (setq gnus-summary-mark-positions pos))))
3512
3513 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3514   "Insert a dummy root in the summary buffer."
3515   (beginning-of-line)
3516   (gnus-add-text-properties
3517    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3518    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3519
3520 (defun gnus-summary-extract-address-component (from)
3521   (or (car (funcall gnus-extract-address-components from))
3522       from))
3523
3524 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3525   (let ((mail-parse-charset gnus-newsgroup-charset)
3526         ; Is it really necessary to do this next part for each summary line?
3527         ; Luckily, doesn't seem to slow things down much.
3528         (mail-parse-ignored-charsets
3529          (with-current-buffer gnus-summary-buffer
3530            gnus-newsgroup-ignored-charsets)))
3531     (or
3532      (and gnus-ignored-from-addresses
3533           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3534           (let ((extra-headers (mail-header-extra header))
3535                 to
3536                 newsgroups)
3537             (cond
3538              ((setq to (cdr (assq 'To extra-headers)))
3539               (concat gnus-summary-to-prefix
3540                       (inline
3541                         (gnus-summary-extract-address-component
3542                          (funcall gnus-decode-encoded-address-function to)))))
3543              ((setq newsgroups
3544                     (or
3545                      (cdr (assq 'Newsgroups extra-headers))
3546                      (and
3547                       (memq 'Newsgroups gnus-extra-headers)
3548                       (eq (car (gnus-find-method-for-group
3549                                 gnus-newsgroup-name)) 'nntp)
3550                       (gnus-group-real-name gnus-newsgroup-name))))
3551               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3552      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3553
3554 (defun gnus-summary-insert-line (gnus-tmp-header
3555                                  gnus-tmp-level gnus-tmp-current
3556                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3557                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3558                                  &optional gnus-tmp-dummy gnus-tmp-score
3559                                  gnus-tmp-process)
3560   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3561          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3562          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3563          (gnus-tmp-score-char
3564           (if (or (null gnus-summary-default-score)
3565                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3566                       gnus-summary-zcore-fuzz))
3567               ?                         ;Whitespace
3568             (if (< gnus-tmp-score gnus-summary-default-score)
3569                 gnus-score-below-mark gnus-score-over-mark)))
3570          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3571          (gnus-tmp-replied
3572           (cond (gnus-tmp-process gnus-process-mark)
3573                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3574                  gnus-cached-mark)
3575                 (gnus-tmp-replied gnus-replied-mark)
3576                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3577                  gnus-forwarded-mark)
3578                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3579                  gnus-saved-mark)
3580                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3581                  gnus-recent-mark)
3582                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3583                  gnus-unseen-mark)
3584                 (t gnus-no-mark)))
3585          (gnus-tmp-downloaded
3586           (cond (undownloaded
3587                  gnus-undownloaded-mark)
3588                 (gnus-newsgroup-agentized
3589                  gnus-downloaded-mark)
3590                 (t
3591                  gnus-no-mark)))
3592          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3593          (gnus-tmp-name
3594           (cond
3595            ((string-match "<[^>]+> *$" gnus-tmp-from)
3596             (let ((beg (match-beginning 0)))
3597               (or (and (string-match "^\".+\"" gnus-tmp-from)
3598                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3599                   (substring gnus-tmp-from 0 beg))))
3600            ((string-match "(.+)" gnus-tmp-from)
3601             (substring gnus-tmp-from
3602                        (1+ (match-beginning 0)) (1- (match-end 0))))
3603            (t gnus-tmp-from)))
3604          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3605          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3606          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3607          (buffer-read-only nil))
3608     (when (string= gnus-tmp-name "")
3609       (setq gnus-tmp-name gnus-tmp-from))
3610     (unless (numberp gnus-tmp-lines)
3611       (setq gnus-tmp-lines -1))
3612     (if (= gnus-tmp-lines -1)
3613         (setq gnus-tmp-lines "?")
3614       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3615       (gnus-put-text-property
3616      (point)
3617      (progn (eval gnus-summary-line-format-spec) (point))
3618        'gnus-number gnus-tmp-number)
3619     (when (gnus-visual-p 'summary-highlight 'highlight)
3620       (forward-line -1)
3621       (gnus-run-hooks 'gnus-summary-update-hook)
3622       (forward-line 1))))
3623
3624 (defun gnus-summary-update-line (&optional dont-update)
3625   "Update summary line after change."
3626   (when (and gnus-summary-default-score
3627              (not gnus-summary-inhibit-highlight))
3628     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3629            (article (gnus-summary-article-number))
3630            (score (gnus-summary-article-score article)))
3631       (unless dont-update
3632         (if (and gnus-summary-mark-below
3633                  (< (gnus-summary-article-score)
3634                     gnus-summary-mark-below))
3635             ;; This article has a low score, so we mark it as read.
3636             (when (memq article gnus-newsgroup-unreads)
3637               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3638           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3639             ;; This article was previously marked as read on account
3640             ;; of a low score, but now it has risen, so we mark it as
3641             ;; unread.
3642             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3643         (gnus-summary-update-mark
3644          (if (or (null gnus-summary-default-score)
3645                  (<= (abs (- score gnus-summary-default-score))
3646                      gnus-summary-zcore-fuzz))
3647              ?                          ;Whitespace
3648            (if (< score gnus-summary-default-score)
3649                gnus-score-below-mark gnus-score-over-mark))
3650          'score))
3651       ;; Do visual highlighting.
3652       (when (gnus-visual-p 'summary-highlight 'highlight)
3653         (gnus-run-hooks 'gnus-summary-update-hook)))))
3654
3655 (defvar gnus-tmp-new-adopts nil)
3656
3657 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3658   "Return the number of articles in THREAD.
3659 This may be 0 in some cases -- if none of the articles in
3660 the thread are to be displayed."
3661   (let* ((number
3662          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3663           (cond
3664            ((not (listp thread))
3665             1)
3666            ((and (consp thread) (cdr thread))
3667             (apply
3668              '+ 1 (mapcar
3669                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3670            ((null thread)
3671             1)
3672            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3673             1)
3674            (t 0))))
3675     (when (and level (zerop level) gnus-tmp-new-adopts)
3676       (incf number
3677             (apply '+ (mapcar
3678                        'gnus-summary-number-of-articles-in-thread
3679                        gnus-tmp-new-adopts))))
3680     (if char
3681         (if (> number 1) gnus-not-empty-thread-mark
3682           gnus-empty-thread-mark)
3683       number)))
3684
3685 (defsubst gnus-summary-line-message-size (head)
3686   "Return pretty-printed version of message size.
3687 This function is intended to be used in
3688 `gnus-summary-line-format-alist'."
3689   (let ((c (or (mail-header-chars head) -1)))
3690     (cond ((< c 0) "n/a")               ; chars not available
3691           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3692           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3693           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3694           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3695
3696
3697 (defun gnus-summary-set-local-parameters (group)
3698   "Go through the local params of GROUP and set all variable specs in that list."
3699   (let ((vars '(quit-config)))          ; Ignore quit-config.
3700     (dolist (elem (gnus-group-find-parameter group))
3701       (and (consp elem)                 ; Has to be a cons.
3702            (consp (cdr elem))           ; The cdr has to be a list.
3703            (symbolp (car elem))         ; Has to be a symbol in there.
3704            (not (memq (car elem) vars))
3705            (ignore-errors               ; So we set it.
3706              (push (car elem) vars)
3707              (make-local-variable (car elem))
3708              (set (car elem) (eval (nth 1 elem))))))))
3709
3710 (defun gnus-summary-read-group (group &optional show-all no-article
3711                                       kill-buffer no-display backward
3712                                       select-articles)
3713   "Start reading news in newsgroup GROUP.
3714 If SHOW-ALL is non-nil, already read articles are also listed.
3715 If NO-ARTICLE is non-nil, no article is selected initially.
3716 If NO-DISPLAY, don't generate a summary buffer."
3717   (let (result)
3718     (while (and group
3719                 (null (setq result
3720                             (let ((gnus-auto-select-next nil))
3721                               (or (gnus-summary-read-group-1
3722                                    group show-all no-article
3723                                    kill-buffer no-display
3724                                    select-articles)
3725                                   (setq show-all nil
3726                                         select-articles nil)))))
3727                 (eq gnus-auto-select-next 'quietly))
3728       (set-buffer gnus-group-buffer)
3729       ;; The entry function called above goes to the next
3730       ;; group automatically, so we go two groups back
3731       ;; if we are searching for the previous group.
3732       (when backward
3733         (gnus-group-prev-unread-group 2))
3734       (if (not (equal group (gnus-group-group-name)))
3735           (setq group (gnus-group-group-name))
3736         (setq group nil)))
3737     result))
3738
3739 (defun gnus-summary-read-group-1 (group show-all no-article
3740                                         kill-buffer no-display
3741                                         &optional select-articles)
3742   ;; Killed foreign groups can't be entered.
3743   ;;  (when (and (not (gnus-group-native-p group))
3744   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3745   ;;    (error "Dead non-native groups can't be entered"))
3746   (gnus-message 5 "Retrieving newsgroup: %s..."
3747                 (gnus-group-decoded-name group))
3748   (let* ((new-group (gnus-summary-setup-buffer group))
3749          (quit-config (gnus-group-quit-config group))
3750          (did-select (and new-group (gnus-select-newsgroup
3751                                      group show-all select-articles))))
3752     (cond
3753      ;; This summary buffer exists already, so we just select it.
3754      ((not new-group)
3755       (gnus-set-global-variables)
3756       (when kill-buffer
3757         (gnus-kill-or-deaden-summary kill-buffer))
3758       (gnus-configure-windows 'summary 'force)
3759       (gnus-set-mode-line 'summary)
3760       (gnus-summary-position-point)
3761       (message "")
3762       t)
3763      ;; We couldn't select this group.
3764      ((null did-select)
3765       (when (and (eq major-mode 'gnus-summary-mode)
3766                  (not (equal (current-buffer) kill-buffer)))
3767         (kill-buffer (current-buffer))
3768         (if (not quit-config)
3769             (progn
3770               ;; Update the info -- marks might need to be removed,
3771               ;; for instance.
3772               (gnus-summary-update-info)
3773               (set-buffer gnus-group-buffer)
3774               (gnus-group-jump-to-group group)
3775               (gnus-group-next-unread-group 1))
3776           (gnus-handle-ephemeral-exit quit-config)))
3777       (let ((grpinfo (gnus-get-info group)))
3778         (if (null (gnus-info-read grpinfo))
3779             (gnus-message 3 "Group %s contains no messages"
3780                           (gnus-group-decoded-name group))
3781           (gnus-message 3 "Can't select group")))
3782       nil)
3783      ;; The user did a `C-g' while prompting for number of articles,
3784      ;; so we exit this group.
3785      ((eq did-select 'quit)
3786       (and (eq major-mode 'gnus-summary-mode)
3787            (not (equal (current-buffer) kill-buffer))
3788            (kill-buffer (current-buffer)))
3789       (when kill-buffer
3790         (gnus-kill-or-deaden-summary kill-buffer))
3791       (if (not quit-config)
3792           (progn
3793             (set-buffer gnus-group-buffer)
3794             (gnus-group-jump-to-group group)
3795             (gnus-group-next-unread-group 1)
3796             (gnus-configure-windows 'group 'force))
3797         (gnus-handle-ephemeral-exit quit-config))
3798       ;; Finally signal the quit.
3799       (signal 'quit nil))
3800      ;; The group was successfully selected.
3801      (t
3802       (gnus-set-global-variables)
3803       ;; Save the active value in effect when the group was entered.
3804       (setq gnus-newsgroup-active
3805             (gnus-copy-sequence
3806              (gnus-active gnus-newsgroup-name)))
3807       ;; You can change the summary buffer in some way with this hook.
3808       (gnus-run-hooks 'gnus-select-group-hook)
3809       (when (memq 'summary (gnus-update-format-specifications
3810                             nil 'summary 'summary-mode 'summary-dummy))
3811         ;; The format specification for the summary line was updated,
3812         ;; so we need to update the mark positions as well.
3813         (gnus-update-summary-mark-positions))
3814       ;; Do score processing.
3815       (when gnus-use-scoring
3816         (gnus-possibly-score-headers))
3817       ;; Check whether to fill in the gaps in the threads.
3818       (when gnus-build-sparse-threads
3819         (gnus-build-sparse-threads))
3820       ;; Find the initial limit.
3821       (if show-all
3822           (let ((gnus-newsgroup-dormant nil))
3823             (gnus-summary-initial-limit show-all))
3824         (gnus-summary-initial-limit show-all))
3825       ;; Generate the summary buffer.
3826       (unless no-display
3827         (gnus-summary-prepare))
3828       (when gnus-use-trees
3829         (gnus-tree-open group)
3830         (setq gnus-summary-highlight-line-function
3831               'gnus-tree-highlight-article))
3832       ;; If the summary buffer is empty, but there are some low-scored
3833       ;; articles or some excluded dormants, we include these in the
3834       ;; buffer.
3835       (when (and (zerop (buffer-size))
3836                  (not no-display))
3837         (cond (gnus-newsgroup-dormant
3838                (gnus-summary-limit-include-dormant))
3839               ((and gnus-newsgroup-scored show-all)
3840                (gnus-summary-limit-include-expunged t))))
3841       ;; Function `gnus-apply-kill-file' must be called in this hook.
3842       (gnus-run-hooks 'gnus-apply-kill-hook)
3843       (if (and (zerop (buffer-size))
3844                (not no-display))
3845           (progn
3846             ;; This newsgroup is empty.
3847             (gnus-summary-catchup-and-exit nil t)
3848             (gnus-message 6 "No unread news")
3849             (when kill-buffer
3850               (gnus-kill-or-deaden-summary kill-buffer))
3851             ;; Return nil from this function.
3852             nil)
3853         ;; Hide conversation thread subtrees.  We cannot do this in
3854         ;; gnus-summary-prepare-hook since kill processing may not
3855         ;; work with hidden articles.
3856         (gnus-summary-maybe-hide-threads)
3857         (when kill-buffer
3858           (gnus-kill-or-deaden-summary kill-buffer))
3859         (gnus-summary-auto-select-subject)
3860         ;; Show first unread article if requested.
3861         (if (and (not no-article)
3862                  (not no-display)
3863                  gnus-newsgroup-unreads
3864                  gnus-auto-select-first)
3865             (progn
3866               (gnus-configure-windows 'summary)
3867               (let ((art (gnus-summary-article-number)))
3868                 (unless (and (not gnus-plugged)
3869                              (or (memq art gnus-newsgroup-undownloaded)
3870                                  (memq art gnus-newsgroup-downloadable)))
3871                   (gnus-summary-goto-article art))))
3872           ;; Don't select any articles.
3873           (gnus-summary-position-point)
3874           (gnus-configure-windows 'summary 'force)
3875           (gnus-set-mode-line 'summary))
3876         (when (and gnus-auto-center-group
3877                    (get-buffer-window gnus-group-buffer t))
3878           ;; Gotta use windows, because recenter does weird stuff if
3879           ;; the current buffer ain't the displayed window.
3880           (let ((owin (selected-window)))
3881             (select-window (get-buffer-window gnus-group-buffer t))
3882             (when (gnus-group-goto-group group)
3883               (recenter))
3884             (select-window owin)))
3885         ;; Mark this buffer as "prepared".
3886         (setq gnus-newsgroup-prepared t)
3887         (gnus-run-hooks 'gnus-summary-prepared-hook)
3888         (unless (gnus-ephemeral-group-p group)
3889           (gnus-group-update-group group))
3890         t)))))
3891
3892 (defun gnus-summary-auto-select-subject ()
3893   "Select the subject line on initial group entry."
3894   (goto-char (point-min))
3895   (cond
3896    ((eq gnus-auto-select-subject 'best)
3897     (gnus-summary-best-unread-subject))
3898    ((eq gnus-auto-select-subject 'unread)
3899     (gnus-summary-first-unread-subject))
3900    ((eq gnus-auto-select-subject 'unseen)
3901     (gnus-summary-first-unseen-subject))
3902    ((eq gnus-auto-select-subject 'unseen-or-unread)
3903     (gnus-summary-first-unseen-or-unread-subject))
3904    ((eq gnus-auto-select-subject 'first)
3905     ;; Do nothing.
3906     )
3907    ((functionp gnus-auto-select-subject)
3908     (funcall gnus-auto-select-subject))))
3909
3910 (defun gnus-summary-prepare ()
3911   "Generate the summary buffer."
3912   (interactive)
3913   (let ((buffer-read-only nil))
3914     (erase-buffer)
3915     (setq gnus-newsgroup-data nil
3916           gnus-newsgroup-data-reverse nil)
3917     (gnus-run-hooks 'gnus-summary-generate-hook)
3918     ;; Generate the buffer, either with threads or without.
3919     (when gnus-newsgroup-headers
3920       (gnus-summary-prepare-threads
3921        (if gnus-show-threads
3922            (gnus-sort-gathered-threads
3923             (funcall gnus-summary-thread-gathering-function
3924                      (gnus-sort-threads
3925                       (gnus-cut-threads (gnus-make-threads)))))
3926          ;; Unthreaded display.
3927          (gnus-sort-articles gnus-newsgroup-headers))))
3928     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3929     ;; Call hooks for modifying summary buffer.
3930     (goto-char (point-min))
3931     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3932
3933 (defsubst gnus-general-simplify-subject (subject)
3934   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3935   (setq subject
3936         (cond
3937          ;; Truncate the subject.
3938          (gnus-simplify-subject-functions
3939           (gnus-map-function gnus-simplify-subject-functions subject))
3940          ((numberp gnus-summary-gather-subject-limit)
3941           (setq subject (gnus-simplify-subject-re subject))
3942           (if (> (length subject) gnus-summary-gather-subject-limit)
3943               (substring subject 0 gnus-summary-gather-subject-limit)
3944             subject))
3945          ;; Fuzzily simplify it.
3946          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3947           (gnus-simplify-subject-fuzzy subject))
3948          ;; Just remove the leading "Re:".
3949          (t
3950           (gnus-simplify-subject-re subject))))
3951
3952   (if (and gnus-summary-gather-exclude-subject
3953            (string-match gnus-summary-gather-exclude-subject subject))
3954       nil                         ; This article shouldn't be gathered
3955     subject))
3956
3957 (defun gnus-summary-simplify-subject-query ()
3958   "Query where the respool algorithm would put this article."
3959   (interactive)
3960   (gnus-summary-select-article)
3961   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3962
3963 (defun gnus-gather-threads-by-subject (threads)
3964   "Gather threads by looking at Subject headers."
3965   (if (not gnus-summary-make-false-root)
3966       threads
3967     (let ((hashtb (gnus-make-hashtable 1024))
3968           (prev threads)
3969           (result threads)
3970           subject hthread whole-subject)
3971       (while threads
3972         (setq subject (gnus-general-simplify-subject
3973                        (setq whole-subject (mail-header-subject
3974                                             (caar threads)))))
3975         (when subject
3976           (if (setq hthread (gnus-gethash subject hashtb))
3977               (progn
3978                 ;; We enter a dummy root into the thread, if we
3979                 ;; haven't done that already.
3980                 (unless (stringp (caar hthread))
3981                   (setcar hthread (list whole-subject (car hthread))))
3982                 ;; We add this new gathered thread to this gathered
3983                 ;; thread.
3984                 (setcdr (car hthread)
3985                         (nconc (cdar hthread) (list (car threads))))
3986                 ;; Remove it from the list of threads.
3987                 (setcdr prev (cdr threads))
3988                 (setq threads prev))
3989             ;; Enter this thread into the hash table.
3990             (gnus-sethash subject
3991                           (if gnus-summary-make-false-root-always
3992                               (progn
3993                                 ;; If you want a dummy root above all
3994                                 ;; threads...
3995                                 (setcar threads (list whole-subject
3996                                                       (car threads)))
3997                                 threads)
3998                             threads)
3999                           hashtb)))
4000         (setq prev threads)
4001         (setq threads (cdr threads)))
4002       result)))
4003
4004 (defun gnus-gather-threads-by-references (threads)
4005   "Gather threads by looking at References headers."
4006   (let ((idhashtb (gnus-make-hashtable 1024))
4007         (thhashtb (gnus-make-hashtable 1024))
4008         (prev threads)
4009         (result threads)
4010         ids references id gthread gid entered ref)
4011     (while threads
4012       (when (setq references (mail-header-references (caar threads)))
4013         (setq id (mail-header-id (caar threads))
4014               ids (inline (gnus-split-references references))
4015               entered nil)
4016         (while (setq ref (pop ids))
4017           (setq ids (delete ref ids))
4018           (if (not (setq gid (gnus-gethash ref idhashtb)))
4019               (progn
4020                 (gnus-sethash ref id idhashtb)
4021                 (gnus-sethash id threads thhashtb))
4022             (setq gthread (gnus-gethash gid thhashtb))
4023             (unless entered
4024               ;; We enter a dummy root into the thread, if we
4025               ;; haven't done that already.
4026               (unless (stringp (caar gthread))
4027                 (setcar gthread (list (mail-header-subject (caar gthread))
4028                                       (car gthread))))
4029               ;; We add this new gathered thread to this gathered
4030               ;; thread.
4031               (setcdr (car gthread)
4032                       (nconc (cdar gthread) (list (car threads)))))
4033             ;; Add it into the thread hash table.
4034             (gnus-sethash id gthread thhashtb)
4035             (setq entered t)
4036             ;; Remove it from the list of threads.
4037             (setcdr prev (cdr threads))
4038             (setq threads prev))))
4039       (setq prev threads)
4040       (setq threads (cdr threads)))
4041     result))
4042
4043 (defun gnus-sort-gathered-threads (threads)
4044   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4045   (let ((result threads))
4046     (while threads
4047       (when (stringp (caar threads))
4048         (setcdr (car threads)
4049                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4050       (setq threads (cdr threads)))
4051     result))
4052
4053 (defun gnus-thread-loop-p (root thread)
4054   "Say whether ROOT is in THREAD."
4055   (let ((stack (list thread))
4056         (infloop 0)
4057         th)
4058     (while (setq thread (pop stack))
4059       (setq th (cdr thread))
4060       (while (and th
4061                   (not (eq (caar th) root)))
4062         (pop th))
4063       (if th
4064           ;; We have found a loop.
4065           (let (ref-dep)
4066             (setcdr thread (delq (car th) (cdr thread)))
4067             (if (boundp (setq ref-dep (intern "none"
4068                                               gnus-newsgroup-dependencies)))
4069                 (setcdr (symbol-value ref-dep)
4070                         (nconc (cdr (symbol-value ref-dep))
4071                                (list (car th))))
4072               (set ref-dep (list nil (car th))))
4073             (setq infloop 1
4074                   stack nil))
4075         ;; Push all the subthreads onto the stack.
4076         (push (cdr thread) stack)))
4077     infloop))
4078
4079 (defun gnus-make-threads ()
4080   "Go through the dependency hashtb and find the roots.  Return all threads."
4081   (let (threads)
4082     (while (catch 'infloop
4083              (mapatoms
4084               (lambda (refs)
4085                 ;; Deal with self-referencing References loops.
4086                 (when (and (car (symbol-value refs))
4087                            (not (zerop
4088                                  (apply
4089                                   '+
4090                                   (mapcar
4091                                    (lambda (thread)
4092                                      (gnus-thread-loop-p
4093                                       (car (symbol-value refs)) thread))
4094                                    (cdr (symbol-value refs)))))))
4095                   (setq threads nil)
4096                   (throw 'infloop t))
4097                 (unless (car (symbol-value refs))
4098                   ;; These threads do not refer back to any other
4099                   ;; articles, so they're roots.
4100                   (setq threads (append (cdr (symbol-value refs)) threads))))
4101               gnus-newsgroup-dependencies)))
4102     threads))
4103
4104 ;; Build the thread tree.
4105 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4106   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4107
4108 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4109 if it was already present.
4110
4111 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4112 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4113 Message-IDs will be renamed to a unique Message-ID before being
4114 entered.
4115
4116 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4117   (let* ((id (mail-header-id header))
4118          (id-dep (and id (intern id dependencies)))
4119          parent-id ref ref-dep ref-header replaced)
4120     ;; Enter this `header' in the `dependencies' table.
4121     (cond
4122      ((not id-dep)
4123       (setq header nil))
4124      ;; The first two cases do the normal part: enter a new `header'
4125      ;; in the `dependencies' table.
4126      ((not (boundp id-dep))
4127       (set id-dep (list header)))
4128      ((null (car (symbol-value id-dep)))
4129       (setcar (symbol-value id-dep) header))
4130
4131      ;; From here the `header' was already present in the
4132      ;; `dependencies' table.
4133      (force-new
4134       ;; Overrides an existing entry;
4135       ;; just set the header part of the entry.
4136       (setcar (symbol-value id-dep) header)
4137       (setq replaced t))
4138
4139      ;; Renames the existing `header' to a unique Message-ID.
4140      ((not gnus-summary-ignore-duplicates)
4141       ;; An article with this Message-ID has already been seen.
4142       ;; We rename the Message-ID.
4143       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4144            (list header))
4145       (mail-header-set-id header id))
4146
4147      ;; The last case ignores an existing entry, except it adds any
4148      ;; additional Xrefs (in case the two articles came from different
4149      ;; servers.
4150      ;; Also sets `header' to `nil' meaning that the `dependencies'
4151      ;; table was *not* modified.
4152      (t
4153       (mail-header-set-xref
4154        (car (symbol-value id-dep))
4155        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4156                    "")
4157                (or (mail-header-xref header) "")))
4158       (setq header nil)))
4159
4160     (when (and header (not replaced))
4161       ;; First check that we are not creating a References loop.
4162       (setq parent-id (gnus-parent-id (mail-header-references header)))
4163       (setq ref parent-id)
4164       (while (and ref
4165                   (setq ref-dep (intern-soft ref dependencies))
4166                   (boundp ref-dep)
4167                   (setq ref-header (car (symbol-value ref-dep))))
4168         (if (string= id ref)
4169             ;; Yuk!  This is a reference loop.  Make the article be a
4170             ;; root article.
4171             (progn
4172               (mail-header-set-references (car (symbol-value id-dep)) "none")
4173               (setq ref nil)
4174               (setq parent-id nil))
4175           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4176       (setq ref-dep (intern (or parent-id "none") dependencies))
4177       (if (boundp ref-dep)
4178           (setcdr (symbol-value ref-dep)
4179                   (nconc (cdr (symbol-value ref-dep))
4180                          (list (symbol-value id-dep))))
4181         (set ref-dep (list nil (symbol-value id-dep)))))
4182     header))
4183
4184 (defun gnus-extract-message-id-from-in-reply-to (string)
4185   (if (string-match "<[^>]+>" string)
4186       (substring string (match-beginning 0) (match-end 0))
4187     nil))
4188
4189 (defun gnus-build-sparse-threads ()
4190   (let ((headers gnus-newsgroup-headers)
4191         (mail-parse-charset gnus-newsgroup-charset)
4192         (gnus-summary-ignore-duplicates t)
4193         header references generation relations
4194         subject child end new-child date)
4195     ;; First we create an alist of generations/relations, where
4196     ;; generations is how much we trust the relation, and the relation
4197     ;; is parent/child.
4198     (gnus-message 7 "Making sparse threads...")
4199     (save-excursion
4200       (nnheader-set-temp-buffer " *gnus sparse threads*")
4201       (while (setq header (pop headers))
4202         (when (and (setq references (mail-header-references header))
4203                    (not (string= references "")))
4204           (insert references)
4205           (setq child (mail-header-id header)
4206                 subject (mail-header-subject header)
4207                 date (mail-header-date header)
4208                 generation 0)
4209           (while (search-backward ">" nil t)
4210             (setq end (1+ (point)))
4211             (when (search-backward "<" nil t)
4212               (setq new-child (buffer-substring (point) end))
4213               (push (list (incf generation)
4214                           child (setq child new-child)
4215                           subject date)
4216                     relations)))
4217           (when child
4218             (push (list (1+ generation) child nil subject) relations))
4219           (erase-buffer)))
4220       (kill-buffer (current-buffer)))
4221     ;; Sort over trustworthiness.
4222     (mapcar
4223      (lambda (relation)
4224        (when (gnus-dependencies-add-header
4225               (make-full-mail-header
4226                gnus-reffed-article-number
4227                (nth 3 relation) "" (or (nth 4 relation) "")
4228                (nth 1 relation)
4229                (or (nth 2 relation) "") 0 0 "")
4230               gnus-newsgroup-dependencies nil)
4231          (push gnus-reffed-article-number gnus-newsgroup-limit)
4232          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4233          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4234                gnus-newsgroup-reads)
4235          (decf gnus-reffed-article-number)))
4236      (sort relations 'car-less-than-car))
4237     (gnus-message 7 "Making sparse threads...done")))
4238
4239 (defun gnus-build-old-threads ()
4240   ;; Look at all the articles that refer back to old articles, and
4241   ;; fetch the headers for the articles that aren't there.  This will
4242   ;; build complete threads - if the roots haven't been expired by the
4243   ;; server, that is.
4244   (let ((mail-parse-charset gnus-newsgroup-charset)
4245         id heads)
4246     (mapatoms
4247      (lambda (refs)
4248        (when (not (car (symbol-value refs)))
4249          (setq heads (cdr (symbol-value refs)))
4250          (while heads
4251            (if (memq (mail-header-number (caar heads))
4252                      gnus-newsgroup-dormant)
4253                (setq heads (cdr heads))
4254              (setq id (symbol-name refs))
4255              (while (and (setq id (gnus-build-get-header id))
4256                          (not (car (gnus-id-to-thread id)))))
4257              (setq heads nil)))))
4258      gnus-newsgroup-dependencies)))
4259
4260 (defsubst gnus-remove-odd-characters (string)
4261   "Translate STRING into something that doesn't contain weird characters."
4262   (mm-subst-char-in-string
4263    ?\r ?\-
4264    (mm-subst-char-in-string ?\n ?\- string t) t))
4265
4266 ;; This function has to be called with point after the article number
4267 ;; on the beginning of the line.
4268 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4269   (let ((eol (point-at-eol))
4270         (buffer (current-buffer))
4271         header references in-reply-to)
4272
4273     ;; overview: [num subject from date id refs chars lines misc]
4274     (unwind-protect
4275         (let (x)
4276           (narrow-to-region (point) eol)
4277           (unless (eobp)
4278             (forward-char))
4279
4280           (setq header
4281                 (make-full-mail-header
4282                  number                 ; number
4283                  (condition-case ()     ; subject
4284                      (gnus-remove-odd-characters
4285                       (funcall gnus-decode-encoded-word-function
4286                                (setq x (nnheader-nov-field))))
4287                    (error x))
4288                  (condition-case ()     ; from
4289                      (gnus-remove-odd-characters
4290                       (funcall gnus-decode-encoded-address-function
4291                                (setq x (nnheader-nov-field))))
4292                    (error x))
4293                  (nnheader-nov-field)   ; date
4294                  (nnheader-nov-read-message-id number)  ; id
4295                  (setq references (nnheader-nov-field)) ; refs
4296                  (nnheader-nov-read-integer) ; chars
4297                  (nnheader-nov-read-integer) ; lines
4298                  (unless (eobp)
4299                    (if (looking-at "Xref: ")
4300                        (goto-char (match-end 0)))
4301                    (nnheader-nov-field)) ; Xref
4302                  (nnheader-nov-parse-extra)))) ; extra
4303
4304       (widen))
4305
4306     (when (and (string= references "")
4307                (setq in-reply-to (mail-header-extra header))
4308                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4309       (mail-header-set-references
4310        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4311
4312     (when gnus-alter-header-function
4313       (funcall gnus-alter-header-function header))
4314     (gnus-dependencies-add-header header dependencies force-new)))
4315
4316 (defun gnus-build-get-header (id)
4317   "Look through the buffer of NOV lines and find the header to ID.
4318 Enter this line into the dependencies hash table, and return
4319 the id of the parent article (if any)."
4320   (let ((deps gnus-newsgroup-dependencies)
4321         found header)
4322     (prog1
4323         (save-excursion
4324           (set-buffer nntp-server-buffer)
4325           (let ((case-fold-search nil))
4326             (goto-char (point-min))
4327             (while (and (not found)
4328                         (search-forward id nil t))
4329               (beginning-of-line)
4330               (setq found (looking-at
4331                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4332                                    (regexp-quote id))))
4333               (or found (beginning-of-line 2)))
4334             (when found
4335               (beginning-of-line)
4336               (and
4337                (setq header (gnus-nov-parse-line
4338                              (read (current-buffer)) deps))
4339                (gnus-parent-id (mail-header-references header))))))
4340       (when header
4341         (let ((number (mail-header-number header)))
4342           (push number gnus-newsgroup-limit)
4343           (push header gnus-newsgroup-headers)
4344           (if (memq number gnus-newsgroup-unselected)
4345               (progn
4346                 (setq gnus-newsgroup-unreads
4347                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4348                                                number))
4349                 (setq gnus-newsgroup-unselected
4350                       (delq number gnus-newsgroup-unselected)))
4351             (push number gnus-newsgroup-ancient)))))))
4352
4353 (defun gnus-build-all-threads ()
4354   "Read all the headers."
4355   (let ((gnus-summary-ignore-duplicates t)
4356         (mail-parse-charset gnus-newsgroup-charset)
4357         (dependencies gnus-newsgroup-dependencies)
4358         header article)
4359     (save-excursion
4360       (set-buffer nntp-server-buffer)
4361       (let ((case-fold-search nil))
4362         (goto-char (point-min))
4363         (while (not (eobp))
4364           (ignore-errors
4365             (setq article (read (current-buffer))
4366                   header (gnus-nov-parse-line article dependencies)))
4367           (when header
4368             (with-current-buffer gnus-summary-buffer
4369               (push header gnus-newsgroup-headers)
4370               (if (memq (setq article (mail-header-number header))
4371                         gnus-newsgroup-unselected)
4372                   (progn
4373                     (setq gnus-newsgroup-unreads
4374                           (gnus-add-to-sorted-list
4375                            gnus-newsgroup-unreads article))
4376                     (setq gnus-newsgroup-unselected
4377                           (delq article gnus-newsgroup-unselected)))
4378                 (push article gnus-newsgroup-ancient)))
4379             (forward-line 1)))))))
4380
4381 (defun gnus-summary-update-article-line (article header)
4382   "Update the line for ARTICLE using HEADER."
4383   (let* ((id (mail-header-id header))
4384          (thread (gnus-id-to-thread id)))
4385     (unless thread
4386       (error "Article in no thread"))
4387     ;; Update the thread.
4388     (setcar thread header)
4389     (gnus-summary-goto-subject article)
4390     (let* ((datal (gnus-data-find-list article))
4391            (data (car datal))
4392            (buffer-read-only nil)
4393            (level (gnus-summary-thread-level)))
4394       (gnus-delete-line)
4395       (let ((inserted (- (point)
4396                          (progn
4397                            (gnus-summary-insert-line
4398                             header level nil
4399                             (memq article gnus-newsgroup-undownloaded)
4400                             (gnus-article-mark article)
4401                             (memq article gnus-newsgroup-replied)
4402                             (memq article gnus-newsgroup-expirable)
4403                             ;; Only insert the Subject string when it's different
4404                             ;; from the previous Subject string.
4405                             (if (and
4406                                  gnus-show-threads
4407                                  (gnus-subject-equal
4408                                   (condition-case ()
4409                                       (mail-header-subject
4410                                        (gnus-data-header
4411                                         (cadr
4412                                          (gnus-data-find-list
4413                                           article
4414                                           (gnus-data-list t)))))
4415                                     ;; Error on the side of excessive subjects.
4416                                     (error ""))
4417                                   (mail-header-subject header)))
4418                                 ""
4419                               (mail-header-subject header))
4420                             nil (cdr (assq article gnus-newsgroup-scored))
4421                             (memq article gnus-newsgroup-processable))
4422                            (point)))))
4423         (when (cdr datal)
4424           (gnus-data-update-list
4425            (cdr datal)
4426            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4427
4428 (defun gnus-summary-update-article (article &optional iheader)
4429   "Update ARTICLE in the summary buffer."
4430   (set-buffer gnus-summary-buffer)
4431   (let* ((header (gnus-summary-article-header article))
4432          (id (mail-header-id header))
4433          (data (gnus-data-find article))
4434          (thread (gnus-id-to-thread id))
4435          (references (mail-header-references header))
4436          (parent
4437           (gnus-id-to-thread
4438            (or (gnus-parent-id
4439                 (when (and references
4440                            (not (equal "" references)))
4441                   references))
4442                "none")))
4443          (buffer-read-only nil)
4444          (old (car thread)))
4445     (when thread
4446       (unless iheader
4447         (setcar thread nil)
4448         (when parent
4449           (delq thread parent)))
4450       (if (gnus-summary-insert-subject id header)
4451           ;; Set the (possibly) new article number in the data structure.
4452           (gnus-data-set-number data (gnus-id-to-article id))
4453         (setcar thread old)
4454         nil))))
4455
4456 (defun gnus-rebuild-thread (id &optional line)
4457   "Rebuild the thread containing ID.
4458 If LINE, insert the rebuilt thread starting on line LINE."
4459   (let ((buffer-read-only nil)
4460         old-pos current thread data)
4461     (if (not gnus-show-threads)
4462         (setq thread (list (car (gnus-id-to-thread id))))
4463       ;; Get the thread this article is part of.
4464       (setq thread (gnus-remove-thread id)))
4465     (setq old-pos (point-at-bol))
4466     (setq current (save-excursion
4467                     (and (re-search-backward "[\r\n]" nil t)
4468                          (gnus-summary-article-number))))
4469     ;; If this is a gathered thread, we have to go some re-gathering.
4470     (when (stringp (car thread))
4471       (let ((subject (car thread))
4472             roots thr)
4473         (setq thread (cdr thread))
4474         (while thread
4475           (unless (memq (setq thr (gnus-id-to-thread
4476                                    (gnus-root-id
4477                                     (mail-header-id (caar thread)))))
4478                         roots)
4479             (push thr roots))
4480           (setq thread (cdr thread)))
4481         ;; We now have all (unique) roots.
4482         (if (= (length roots) 1)
4483             ;; All the loose roots are now one solid root.
4484             (setq thread (car roots))
4485           (setq thread (cons subject (gnus-sort-threads roots))))))
4486     (let (threads)
4487       ;; We then insert this thread into the summary buffer.
4488       (when line
4489         (goto-char (point-min))
4490         (forward-line (1- line)))
4491       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4492         (if gnus-show-threads
4493             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4494           (gnus-summary-prepare-unthreaded thread))
4495         (setq data (nreverse gnus-newsgroup-data))
4496         (setq threads gnus-newsgroup-threads))
4497       ;; We splice the new data into the data structure.
4498       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4499       ;;!!! then we want to insert at the beginning of the buffer.
4500       ;;!!! That happens to be true with Gnus now, but that may
4501       ;;!!! change in the future.  Perhaps.
4502       (gnus-data-enter-list
4503        (if line nil current) data (- (point) old-pos))
4504       (setq gnus-newsgroup-threads
4505             (nconc threads gnus-newsgroup-threads))
4506       (gnus-data-compute-positions))))
4507
4508 (defun gnus-number-to-header (number)
4509   "Return the header for article NUMBER."
4510   (let ((headers gnus-newsgroup-headers))
4511     (while (and headers
4512                 (not (= number (mail-header-number (car headers)))))
4513       (pop headers))
4514     (when headers
4515       (car headers))))
4516
4517 (defun gnus-parent-headers (in-headers &optional generation)
4518   "Return the headers of the GENERATIONeth parent of HEADERS."
4519   (unless generation
4520     (setq generation 1))
4521   (let ((parent t)
4522         (headers in-headers)
4523         references)
4524     (while (and parent
4525                 (not (zerop generation))
4526                 (setq references (mail-header-references headers)))
4527       (setq headers (if (and references
4528                              (setq parent (gnus-parent-id references)))
4529                         (car (gnus-id-to-thread parent))
4530                       nil))
4531       (decf generation))
4532     (and (not (eq headers in-headers))
4533          headers)))
4534
4535 (defun gnus-id-to-thread (id)
4536   "Return the (sub-)thread where ID appears."
4537   (gnus-gethash id gnus-newsgroup-dependencies))
4538
4539 (defun gnus-id-to-article (id)
4540   "Return the article number of ID."
4541   (let ((thread (gnus-id-to-thread id)))
4542     (when (and thread
4543                (car thread))
4544       (mail-header-number (car thread)))))
4545
4546 (defun gnus-id-to-header (id)
4547   "Return the article headers of ID."
4548   (car (gnus-id-to-thread id)))
4549
4550 (defun gnus-article-displayed-root-p (article)
4551   "Say whether ARTICLE is a root(ish) article."
4552   (let ((level (gnus-summary-thread-level article))
4553         (refs (mail-header-references  (gnus-summary-article-header article)))
4554         particle)
4555     (cond
4556      ((null level) nil)
4557      ((zerop level) t)
4558      ((null refs) t)
4559      ((null (gnus-parent-id refs)) t)
4560      ((and (= 1 level)
4561            (null (setq particle (gnus-id-to-article
4562                                  (gnus-parent-id refs))))
4563            (null (gnus-summary-thread-level particle)))))))
4564
4565 (defun gnus-root-id (id)
4566   "Return the id of the root of the thread where ID appears."
4567   (let (last-id prev)
4568     (while (and id (setq prev (car (gnus-id-to-thread id))))
4569       (setq last-id id
4570             id (gnus-parent-id (mail-header-references prev))))
4571     last-id))
4572
4573 (defun gnus-articles-in-thread (thread)
4574   "Return the list of articles in THREAD."
4575   (cons (mail-header-number (car thread))
4576         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4577
4578 (defun gnus-remove-thread (id &optional dont-remove)
4579   "Remove the thread that has ID in it."
4580   (let (headers thread last-id)
4581     ;; First go up in this thread until we find the root.
4582     (setq last-id (gnus-root-id id)
4583           headers (message-flatten-list (gnus-id-to-thread last-id)))
4584     ;; We have now found the real root of this thread.  It might have
4585     ;; been gathered into some loose thread, so we have to search
4586     ;; through the threads to find the thread we wanted.
4587     (let ((threads gnus-newsgroup-threads)
4588           sub)
4589       (while threads
4590         (setq sub (car threads))
4591         (if (stringp (car sub))
4592             ;; This is a gathered thread, so we look at the roots
4593             ;; below it to find whether this article is in this
4594             ;; gathered root.
4595             (progn
4596               (setq sub (cdr sub))
4597               (while sub
4598                 (when (member (caar sub) headers)
4599                   (setq thread (car threads)
4600                         threads nil
4601                         sub nil))
4602                 (setq sub (cdr sub))))
4603           ;; It's an ordinary thread, so we check it.
4604           (when (eq (car sub) (car headers))
4605             (setq thread sub
4606                   threads nil)))
4607         (setq threads (cdr threads)))
4608       ;; If this article is in no thread, then it's a root.
4609       (if thread
4610           (unless dont-remove
4611             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4612         (setq thread (gnus-id-to-thread last-id)))
4613       (when thread
4614         (prog1
4615             thread                      ; We return this thread.
4616           (unless dont-remove
4617             (if (stringp (car thread))
4618                 (progn
4619                   ;; If we use dummy roots, then we have to remove the
4620                   ;; dummy root as well.
4621                   (when (eq gnus-summary-make-false-root 'dummy)
4622                     ;; We go to the dummy root by going to
4623                     ;; the first sub-"thread", and then one line up.
4624                     (gnus-summary-goto-article
4625                      (mail-header-number (caadr thread)))
4626                     (forward-line -1)
4627                     (gnus-delete-line)
4628                     (gnus-data-compute-positions))
4629                   (setq thread (cdr thread))
4630                   (while thread
4631                     (gnus-remove-thread-1 (car thread))
4632                     (setq thread (cdr thread))))
4633               (gnus-remove-thread-1 thread))))))))
4634
4635 (defun gnus-remove-thread-1 (thread)
4636   "Remove the thread THREAD recursively."
4637   (let ((number (mail-header-number (pop thread)))
4638         d)
4639     (setq thread (reverse thread))
4640     (while thread
4641       (gnus-remove-thread-1 (pop thread)))
4642     (when (setq d (gnus-data-find number))
4643       (goto-char (gnus-data-pos d))
4644       (gnus-summary-show-thread)
4645       (gnus-data-remove
4646        number
4647        (- (point-at-bol)
4648           (prog1
4649               (1+ (point-at-eol))
4650             (gnus-delete-line)))))))
4651
4652 (defun gnus-sort-threads-1 (threads func)
4653   (sort (mapcar (lambda (thread)
4654                   (cons (car thread)
4655                         (and (cdr thread)
4656                              (gnus-sort-threads-1 (cdr thread) func))))
4657                 threads) func))
4658
4659 (defun gnus-sort-threads (threads)
4660   "Sort THREADS."
4661   (if (not gnus-thread-sort-functions)
4662       threads
4663     (gnus-message 8 "Sorting threads...")
4664     (let ((max-lisp-eval-depth 5000))
4665       (prog1 (gnus-sort-threads-1
4666          threads
4667          (gnus-make-sort-function gnus-thread-sort-functions))
4668         (gnus-message 8 "Sorting threads...done")))))
4669
4670 (defun gnus-sort-articles (articles)
4671   "Sort ARTICLES."
4672   (when gnus-article-sort-functions
4673     (gnus-message 7 "Sorting articles...")
4674     (prog1
4675         (setq gnus-newsgroup-headers
4676               (sort articles (gnus-make-sort-function
4677                               gnus-article-sort-functions)))
4678       (gnus-message 7 "Sorting articles...done"))))
4679
4680 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4681 (defmacro gnus-thread-header (thread)
4682   "Return header of first article in THREAD.
4683 Note that THREAD must never, ever be anything else than a variable -
4684 using some other form will lead to serious barfage."
4685   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4686   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4687   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4688         (vector thread) 2))
4689
4690 (defsubst gnus-article-sort-by-number (h1 h2)
4691   "Sort articles by article number."
4692   (< (mail-header-number h1)
4693      (mail-header-number h2)))
4694
4695 (defun gnus-thread-sort-by-number (h1 h2)
4696   "Sort threads by root article number."
4697   (gnus-article-sort-by-number
4698    (gnus-thread-header h1) (gnus-thread-header h2)))
4699
4700 (defsubst gnus-article-sort-by-random (h1 h2)
4701   "Sort articles by article number."
4702   (zerop (random 2)))
4703
4704 (defun gnus-thread-sort-by-random (h1 h2)
4705   "Sort threads by root article number."
4706   (gnus-article-sort-by-random
4707    (gnus-thread-header h1) (gnus-thread-header h2)))
4708
4709 (defsubst gnus-article-sort-by-lines (h1 h2)
4710   "Sort articles by article Lines header."
4711   (< (mail-header-lines h1)
4712      (mail-header-lines h2)))
4713
4714 (defun gnus-thread-sort-by-lines (h1 h2)
4715   "Sort threads by root article Lines header."
4716   (gnus-article-sort-by-lines
4717    (gnus-thread-header h1) (gnus-thread-header h2)))
4718
4719 (defsubst gnus-article-sort-by-chars (h1 h2)
4720   "Sort articles by octet length."
4721   (< (mail-header-chars h1)
4722      (mail-header-chars h2)))
4723
4724 (defun gnus-thread-sort-by-chars (h1 h2)
4725   "Sort threads by root article octet length."
4726   (gnus-article-sort-by-chars
4727    (gnus-thread-header h1) (gnus-thread-header h2)))
4728
4729 (defsubst gnus-article-sort-by-author (h1 h2)
4730   "Sort articles by root author."
4731   (string-lessp
4732    (let ((extract (funcall
4733                    gnus-extract-address-components
4734                    (mail-header-from h1))))
4735      (or (car extract) (cadr extract) ""))
4736    (let ((extract (funcall
4737                    gnus-extract-address-components
4738                    (mail-header-from h2))))
4739      (or (car extract) (cadr extract) ""))))
4740
4741 (defun gnus-thread-sort-by-author (h1 h2)
4742   "Sort threads by root author."
4743   (gnus-article-sort-by-author
4744    (gnus-thread-header h1)  (gnus-thread-header h2)))
4745
4746 (defsubst gnus-article-sort-by-recipient (h1 h2)
4747   "Sort articles by recipient."
4748   (string-lessp
4749    (let ((extract (funcall
4750                    gnus-extract-address-components
4751                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4752      (or (car extract) (cadr extract)))
4753    (let ((extract (funcall
4754                    gnus-extract-address-components
4755                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4756      (or (car extract) (cadr extract)))))
4757
4758 (defun gnus-thread-sort-by-recipient (h1 h2)
4759   "Sort threads by root recipient."
4760   (gnus-article-sort-by-recipient
4761    (gnus-thread-header h1) (gnus-thread-header h2)))
4762
4763 (defsubst gnus-article-sort-by-subject (h1 h2)
4764   "Sort articles by root subject."
4765   (string-lessp
4766    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4767    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4768
4769 (defun gnus-thread-sort-by-subject (h1 h2)
4770   "Sort threads by root subject."
4771   (gnus-article-sort-by-subject
4772    (gnus-thread-header h1) (gnus-thread-header h2)))
4773
4774 (defsubst gnus-article-sort-by-date (h1 h2)
4775   "Sort articles by root article date."
4776   (time-less-p
4777    (gnus-date-get-time (mail-header-date h1))
4778    (gnus-date-get-time (mail-header-date h2))))
4779
4780 (defun gnus-thread-sort-by-date (h1 h2)
4781   "Sort threads by root article date."
4782   (gnus-article-sort-by-date
4783    (gnus-thread-header h1) (gnus-thread-header h2)))
4784
4785 (defsubst gnus-article-sort-by-score (h1 h2)
4786   "Sort articles by root article score.
4787 Unscored articles will be counted as having a score of zero."
4788   (> (or (cdr (assq (mail-header-number h1)
4789                     gnus-newsgroup-scored))
4790          gnus-summary-default-score 0)
4791      (or (cdr (assq (mail-header-number h2)
4792                     gnus-newsgroup-scored))
4793          gnus-summary-default-score 0)))
4794
4795 (defun gnus-thread-sort-by-score (h1 h2)
4796   "Sort threads by root article score."
4797   (gnus-article-sort-by-score
4798    (gnus-thread-header h1) (gnus-thread-header h2)))
4799
4800 (defun gnus-thread-sort-by-total-score (h1 h2)
4801   "Sort threads by the sum of all scores in the thread.
4802 Unscored articles will be counted as having a score of zero."
4803   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4804
4805 (defun gnus-thread-total-score (thread)
4806   ;; This function find the total score of THREAD.
4807   (cond
4808    ((null thread)
4809     0)
4810    ((consp thread)
4811     (if (stringp (car thread))
4812         (apply gnus-thread-score-function 0
4813                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4814       (gnus-thread-total-score-1 thread)))
4815    (t
4816     (gnus-thread-total-score-1 (list thread)))))
4817
4818 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4819   "Sort threads such that the thread with the most recently arrived article comes first."
4820   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4821
4822 (defun gnus-thread-highest-number (thread)
4823   "Return the highest article number in THREAD."
4824   (apply 'max (mapcar (lambda (header)
4825                         (mail-header-number header))
4826                       (message-flatten-list thread))))
4827
4828 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4829   "Sort threads such that the thread with the most recently dated article comes first."
4830   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4831
4832 (defun gnus-thread-latest-date (thread)
4833   "Return the highest article date in THREAD."
4834   (let ((previous-time 0))
4835     (apply 'max
4836            (mapcar
4837             (lambda (header)
4838               (setq previous-time
4839                     (condition-case ()
4840                         (time-to-seconds (mail-header-parse-date
4841                                           (mail-header-date header)))
4842                       (error previous-time))))
4843             (sort
4844              (message-flatten-list thread)
4845              (lambda (h1 h2)
4846                (< (mail-header-number h1)
4847                   (mail-header-number h2))))))))
4848
4849 (defun gnus-thread-total-score-1 (root)
4850   ;; This function find the total score of the thread below ROOT.
4851   (setq root (car root))
4852   (apply gnus-thread-score-function
4853          (or (append
4854               (mapcar 'gnus-thread-total-score
4855                       (cdr (gnus-id-to-thread (mail-header-id root))))
4856               (when (> (mail-header-number root) 0)
4857                 (list (or (cdr (assq (mail-header-number root)
4858                                      gnus-newsgroup-scored))
4859                           gnus-summary-default-score 0))))
4860              (list gnus-summary-default-score)
4861              '(0))))
4862
4863 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4864 (defvar gnus-tmp-prev-subject nil)
4865 (defvar gnus-tmp-false-parent nil)
4866 (defvar gnus-tmp-root-expunged nil)
4867 (defvar gnus-tmp-dummy-line nil)
4868
4869 (eval-when-compile (defvar gnus-tmp-header))
4870 (defun gnus-extra-header (type &optional header)
4871   "Return the extra header of TYPE."
4872   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4873       ""))
4874
4875 (defvar gnus-tmp-thread-tree-header-string "")
4876
4877 (defcustom gnus-sum-thread-tree-root "> "
4878   "With %B spec, used for the root of a thread.
4879 If nil, use subject instead."
4880   :version "22.1"
4881   :type '(radio (const :format "%v  " nil) string)
4882   :group 'gnus-thread)
4883
4884 (defcustom gnus-sum-thread-tree-false-root "> "
4885   "With %B spec, used for a false root of a thread.
4886 If nil, use subject instead."
4887   :version "22.1"
4888   :type '(radio (const :format "%v  " nil) string)
4889   :group 'gnus-thread)
4890
4891 (defcustom gnus-sum-thread-tree-single-indent ""
4892   "With %B spec, used for a thread with just one message.
4893 If nil, use subject instead."
4894   :version "22.1"
4895   :type '(radio (const :format "%v  " nil) string)
4896   :group 'gnus-thread)
4897
4898 (defcustom gnus-sum-thread-tree-vertical "| "
4899   "With %B spec, used for drawing a vertical line."
4900   :version "22.1"
4901   :type 'string
4902   :group 'gnus-thread)
4903
4904 (defcustom gnus-sum-thread-tree-indent "  "
4905   "With %B spec, used for indenting."
4906   :version "22.1"
4907   :type 'string
4908   :group 'gnus-thread)
4909
4910 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4911   "With %B spec, used for a leaf with brothers."
4912   :version "22.1"
4913   :type 'string
4914   :group 'gnus-thread)
4915
4916 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4917   "With %B spec, used for a leaf without brothers."
4918   :version "22.1"
4919   :type 'string
4920   :group 'gnus-thread)
4921
4922 (defun gnus-summary-prepare-threads (threads)
4923   "Prepare summary buffer from THREADS and indentation LEVEL.
4924 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4925 or a straight list of headers."
4926   (gnus-message 7 "Generating summary...")
4927
4928   (setq gnus-newsgroup-threads threads)
4929   (beginning-of-line)
4930
4931   (let ((gnus-tmp-level 0)
4932         (default-score (or gnus-summary-default-score 0))
4933         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4934         (building-line-count gnus-summary-display-while-building)
4935         (building-count (integerp gnus-summary-display-while-building))
4936         thread number subject stack state gnus-tmp-gathered beg-match
4937         new-roots gnus-tmp-new-adopts thread-end simp-subject
4938         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4939         gnus-tmp-replied gnus-tmp-subject-or-nil
4940         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4941         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4942         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4943         tree-stack)
4944
4945     (setq gnus-tmp-prev-subject nil
4946           gnus-tmp-thread-tree-header-string "")
4947
4948     (if (vectorp (car threads))
4949         ;; If this is a straight (sic) list of headers, then a
4950         ;; threaded summary display isn't required, so we just create
4951         ;; an unthreaded one.
4952         (gnus-summary-prepare-unthreaded threads)
4953
4954       ;; Do the threaded display.
4955
4956       (if gnus-summary-display-while-building
4957           (switch-to-buffer (buffer-name)))
4958       (while (or threads stack gnus-tmp-new-adopts new-roots)
4959
4960         (if (and (= gnus-tmp-level 0)
4961                  (or (not stack)
4962                      (= (caar stack) 0))
4963                  (not gnus-tmp-false-parent)
4964                  (or gnus-tmp-new-adopts new-roots))
4965             (if gnus-tmp-new-adopts
4966                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4967                       thread (list (car gnus-tmp-new-adopts))
4968                       gnus-tmp-header (caar thread)
4969                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4970               (when new-roots
4971                 (setq thread (list (car new-roots))
4972                       gnus-tmp-header (caar thread)
4973                       new-roots (cdr new-roots))))
4974
4975           (if threads
4976               ;; If there are some threads, we do them before the
4977               ;; threads on the stack.
4978               (setq thread threads
4979                     gnus-tmp-header (caar thread))
4980             ;; There were no current threads, so we pop something off
4981             ;; the stack.
4982             (setq state (car stack)
4983                   gnus-tmp-level (car state)
4984                   tree-stack (cadr state)
4985                   thread (caddr state)
4986                   stack (cdr stack)
4987                   gnus-tmp-header (caar thread))))
4988
4989         (setq gnus-tmp-false-parent nil)
4990         (setq gnus-tmp-root-expunged nil)
4991         (setq thread-end nil)
4992
4993         (if (stringp gnus-tmp-header)
4994             ;; The header is a dummy root.
4995             (cond
4996              ((eq gnus-summary-make-false-root 'adopt)
4997               ;; We let the first article adopt the rest.
4998               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4999                                                (cddar thread)))
5000               (setq gnus-tmp-gathered
5001                     (nconc (mapcar
5002                             (lambda (h) (mail-header-number (car h)))
5003                             (cddar thread))
5004                            gnus-tmp-gathered))
5005               (setq thread (cons (list (caar thread)
5006                                        (cadar thread))
5007                                  (cdr thread)))
5008               (setq gnus-tmp-level -1
5009                     gnus-tmp-false-parent t))
5010              ((eq gnus-summary-make-false-root 'empty)
5011               ;; We print adopted articles with empty subject fields.
5012               (setq gnus-tmp-gathered
5013                     (nconc (mapcar
5014                             (lambda (h) (mail-header-number (car h)))
5015                             (cddar thread))
5016                            gnus-tmp-gathered))
5017               (setq gnus-tmp-level -1))
5018              ((eq gnus-summary-make-false-root 'dummy)
5019               ;; We remember that we probably want to output a dummy
5020               ;; root.
5021               (setq gnus-tmp-dummy-line gnus-tmp-header)
5022               (setq gnus-tmp-prev-subject gnus-tmp-header))
5023              (t
5024               ;; We do not make a root for the gathered
5025               ;; sub-threads at all.
5026               (setq gnus-tmp-level -1)))
5027
5028           (setq number (mail-header-number gnus-tmp-header)
5029                 subject (mail-header-subject gnus-tmp-header)
5030                 simp-subject (gnus-simplify-subject-fully subject))
5031
5032           (cond
5033            ;; If the thread has changed subject, we might want to make
5034            ;; this subthread into a root.
5035            ((and (null gnus-thread-ignore-subject)
5036                  (not (zerop gnus-tmp-level))
5037                  gnus-tmp-prev-subject
5038                  (not (string= gnus-tmp-prev-subject simp-subject)))
5039             (setq new-roots (nconc new-roots (list (car thread)))
5040                   thread-end t
5041                   gnus-tmp-header nil))
5042            ;; If the article lies outside the current limit,
5043            ;; then we do not display it.
5044            ((not (memq number gnus-newsgroup-limit))
5045             (setq gnus-tmp-gathered
5046                   (nconc (mapcar
5047                           (lambda (h) (mail-header-number (car h)))
5048                           (cdar thread))
5049                          gnus-tmp-gathered))
5050             (setq gnus-tmp-new-adopts (if (cdar thread)
5051                                           (append gnus-tmp-new-adopts
5052                                                   (cdar thread))
5053                                         gnus-tmp-new-adopts)
5054                   thread-end t
5055                   gnus-tmp-header nil)
5056             (when (zerop gnus-tmp-level)
5057               (setq gnus-tmp-root-expunged t)))
5058            ;; Perhaps this article is to be marked as read?
5059            ((and gnus-summary-mark-below
5060                  (< (or (cdr (assq number gnus-newsgroup-scored))
5061                         default-score)
5062                     gnus-summary-mark-below)
5063                  ;; Don't touch sparse articles.
5064                  (not (gnus-summary-article-sparse-p number))
5065                  (not (gnus-summary-article-ancient-p number)))
5066             (setq gnus-newsgroup-unreads
5067                   (delq number gnus-newsgroup-unreads))
5068             (if gnus-newsgroup-auto-expire
5069                 (setq gnus-newsgroup-expirable
5070                       (gnus-add-to-sorted-list
5071                        gnus-newsgroup-expirable number))
5072               (push (cons number gnus-low-score-mark)
5073                     gnus-newsgroup-reads))))
5074
5075           (when gnus-tmp-header
5076             ;; We may have an old dummy line to output before this
5077             ;; article.
5078             (when (and gnus-tmp-dummy-line
5079                        (gnus-subject-equal
5080                         gnus-tmp-dummy-line
5081                         (mail-header-subject gnus-tmp-header)))
5082               (gnus-summary-insert-dummy-line
5083                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5084               (setq gnus-tmp-dummy-line nil))
5085
5086             ;; Compute the mark.
5087             (setq gnus-tmp-unread (gnus-article-mark number))
5088
5089             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5090                                   gnus-tmp-header gnus-tmp-level)
5091                   gnus-newsgroup-data)
5092
5093             ;; Actually insert the line.
5094             (setq
5095              gnus-tmp-subject-or-nil
5096              (cond
5097               ((and gnus-thread-ignore-subject
5098                     gnus-tmp-prev-subject
5099                     (not (string= gnus-tmp-prev-subject simp-subject)))
5100                subject)
5101               ((zerop gnus-tmp-level)
5102                (if (and (eq gnus-summary-make-false-root 'empty)
5103                         (memq number gnus-tmp-gathered)
5104                         gnus-tmp-prev-subject
5105                         (string= gnus-tmp-prev-subject simp-subject))
5106                    gnus-summary-same-subject
5107                  subject))
5108               (t gnus-summary-same-subject)))
5109             (if (and (eq gnus-summary-make-false-root 'adopt)
5110                      (= gnus-tmp-level 1)
5111                      (memq number gnus-tmp-gathered))
5112                 (setq gnus-tmp-opening-bracket ?\<
5113                       gnus-tmp-closing-bracket ?\>)
5114               (setq gnus-tmp-opening-bracket ?\[
5115                     gnus-tmp-closing-bracket ?\]))
5116             (setq
5117              gnus-tmp-indentation
5118              (aref gnus-thread-indent-array gnus-tmp-level)
5119              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5120              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5121                                 gnus-summary-default-score 0)
5122              gnus-tmp-score-char
5123              (if (or (null gnus-summary-default-score)
5124                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5125                          gnus-summary-zcore-fuzz))
5126                  ?                      ;Whitespace
5127                (if (< gnus-tmp-score gnus-summary-default-score)
5128                    gnus-score-below-mark gnus-score-over-mark))
5129              gnus-tmp-replied
5130              (cond ((memq number gnus-newsgroup-processable)
5131                     gnus-process-mark)
5132                    ((memq number gnus-newsgroup-cached)
5133                     gnus-cached-mark)
5134                    ((memq number gnus-newsgroup-replied)
5135                     gnus-replied-mark)
5136                    ((memq number gnus-newsgroup-forwarded)
5137                     gnus-forwarded-mark)
5138                    ((memq number gnus-newsgroup-saved)
5139                     gnus-saved-mark)
5140                    ((memq number gnus-newsgroup-recent)
5141                     gnus-recent-mark)
5142                    ((memq number gnus-newsgroup-unseen)
5143                     gnus-unseen-mark)
5144                    (t gnus-no-mark))
5145              gnus-tmp-downloaded
5146              (cond ((memq number gnus-newsgroup-undownloaded)
5147                     gnus-undownloaded-mark)
5148                    (gnus-newsgroup-agentized
5149                     gnus-downloaded-mark)
5150                    (t
5151                     gnus-no-mark))
5152              gnus-tmp-from (mail-header-from gnus-tmp-header)
5153              gnus-tmp-name
5154              (cond
5155               ((string-match "<[^>]+> *$" gnus-tmp-from)
5156                (setq beg-match (match-beginning 0))
5157                (or (and (string-match "^\".+\"" gnus-tmp-from)
5158                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5159                    (substring gnus-tmp-from 0 beg-match)))
5160               ((string-match "(.+)" gnus-tmp-from)
5161                (substring gnus-tmp-from
5162                           (1+ (match-beginning 0)) (1- (match-end 0))))
5163               (t gnus-tmp-from))
5164
5165              ;; Do the %B string
5166              gnus-tmp-thread-tree-header-string
5167              (cond
5168               ((not gnus-show-threads) "")
5169               ((zerop gnus-tmp-level)
5170                (cond ((cdar thread)
5171                       (or gnus-sum-thread-tree-root subject))
5172                      (gnus-tmp-new-adopts
5173                       (or gnus-sum-thread-tree-false-root subject))
5174                      (t
5175                       (or gnus-sum-thread-tree-single-indent subject))))
5176               (t
5177                (concat (apply 'concat
5178                               (mapcar (lambda (item)
5179                                         (if (= item 1)
5180                                             gnus-sum-thread-tree-vertical
5181                                           gnus-sum-thread-tree-indent))
5182                                       (cdr (reverse tree-stack))))
5183                        (if (nth 1 thread)
5184                            gnus-sum-thread-tree-leaf-with-other
5185                          gnus-sum-thread-tree-single-leaf)))))
5186             (when (string= gnus-tmp-name "")
5187               (setq gnus-tmp-name gnus-tmp-from))
5188             (unless (numberp gnus-tmp-lines)
5189               (setq gnus-tmp-lines -1))
5190             (if (= gnus-tmp-lines -1)
5191                 (setq gnus-tmp-lines "?")
5192               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5193               (gnus-put-text-property
5194              (point)
5195              (progn (eval gnus-summary-line-format-spec) (point))
5196                'gnus-number number)
5197             (when gnus-visual-p
5198               (forward-line -1)
5199               (gnus-run-hooks 'gnus-summary-update-hook)
5200               (forward-line 1))
5201
5202             (setq gnus-tmp-prev-subject simp-subject)))
5203
5204         (when (nth 1 thread)
5205           (push (list (max 0 gnus-tmp-level)
5206                       (copy-sequence tree-stack)
5207                       (nthcdr 1 thread))
5208                 stack))
5209         (push (if (nth 1 thread) 1 0) tree-stack)
5210         (incf gnus-tmp-level)
5211         (setq threads (if thread-end nil (cdar thread)))
5212         (if gnus-summary-display-while-building
5213             (if building-count
5214                 (progn
5215                   ;; use a set frequency
5216                   (setq building-line-count (1- building-line-count))
5217                   (when (= building-line-count 0)
5218                     (sit-for 0)
5219                     (setq building-line-count
5220                           gnus-summary-display-while-building)))
5221               ;; always
5222               (sit-for 0)))
5223         (unless threads
5224           (setq gnus-tmp-level 0)))))
5225   (gnus-message 7 "Generating summary...done"))
5226
5227 (defun gnus-summary-prepare-unthreaded (headers)
5228   "Generate an unthreaded summary buffer based on HEADERS."
5229   (let (header number mark)
5230
5231     (beginning-of-line)
5232
5233     (while headers
5234       ;; We may have to root out some bad articles...
5235       (when (memq (setq number (mail-header-number
5236                                 (setq header (pop headers))))
5237                   gnus-newsgroup-limit)
5238         ;; Mark article as read when it has a low score.
5239         (when (and gnus-summary-mark-below
5240                    (< (or (cdr (assq number gnus-newsgroup-scored))
5241                           gnus-summary-default-score 0)
5242                       gnus-summary-mark-below)
5243                    (not (gnus-summary-article-ancient-p number)))
5244           (setq gnus-newsgroup-unreads
5245                 (delq number gnus-newsgroup-unreads))
5246           (if gnus-newsgroup-auto-expire
5247               (push number gnus-newsgroup-expirable)
5248             (push (cons number gnus-low-score-mark)
5249                   gnus-newsgroup-reads)))
5250
5251         (setq mark (gnus-article-mark number))
5252         (push (gnus-data-make number mark (1+ (point)) header 0)
5253               gnus-newsgroup-data)
5254         (gnus-summary-insert-line
5255          header 0 number
5256          (memq number gnus-newsgroup-undownloaded)
5257          mark (memq number gnus-newsgroup-replied)
5258          (memq number gnus-newsgroup-expirable)
5259          (mail-header-subject header) nil
5260          (cdr (assq number gnus-newsgroup-scored))
5261          (memq number gnus-newsgroup-processable))))))
5262
5263 (defun gnus-summary-remove-list-identifiers ()
5264   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5265   (let ((regexp (if (consp gnus-list-identifiers)
5266                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5267                   gnus-list-identifiers))
5268         changed subject)
5269     (when regexp
5270       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5271       (dolist (header gnus-newsgroup-headers)
5272         (setq subject (mail-header-subject header)
5273               changed nil)
5274         (while (string-match regexp subject)
5275           (setq subject
5276                 (concat (substring subject 0 (match-beginning 1))
5277                         (substring subject (match-end 0)))
5278                 changed t))
5279         (when changed
5280           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5281             (setq subject
5282                   (concat (substring subject 0 (match-beginning 1))
5283                           (substring subject (match-end 1)))))
5284           (mail-header-set-subject header subject))))))
5285
5286 (defun gnus-fetch-headers (articles)
5287   "Fetch headers of ARTICLES."
5288   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5289     (gnus-message 5 "Fetching headers for %s..." name)
5290     (prog1
5291         (if (eq 'nov
5292                 (setq gnus-headers-retrieved-by
5293                       (gnus-retrieve-headers
5294                        articles gnus-newsgroup-name
5295                        ;; We might want to fetch old headers, but
5296                        ;; not if there is only 1 article.
5297                        (and (or (and
5298                                  (not (eq gnus-fetch-old-headers 'some))
5299                                  (not (numberp gnus-fetch-old-headers)))
5300                                 (> (length articles) 1))
5301                             gnus-fetch-old-headers))))
5302             (gnus-get-newsgroup-headers-xover
5303              articles nil nil gnus-newsgroup-name t)
5304           (gnus-get-newsgroup-headers))
5305       (gnus-message 5 "Fetching headers for %s...done" name))))
5306
5307 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5308   "Select newsgroup GROUP.
5309 If READ-ALL is non-nil, all articles in the group are selected.
5310 If SELECT-ARTICLES, only select those articles from GROUP."
5311   (let* ((entry (gnus-group-entry group))
5312          ;;!!! Dirty hack; should be removed.
5313          (gnus-summary-ignore-duplicates
5314           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5315               t
5316             gnus-summary-ignore-duplicates))
5317          (info (nth 2 entry))
5318          articles fetched-articles cached)
5319
5320     (unless (gnus-check-server
5321              (set (make-local-variable 'gnus-current-select-method)
5322                   (gnus-find-method-for-group group)))
5323       (error "Couldn't open server"))
5324
5325     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5326         (gnus-activate-group group)     ; Or we can activate it...
5327         (progn                          ; Or we bug out.
5328           (when (equal major-mode 'gnus-summary-mode)
5329             (gnus-kill-buffer (current-buffer)))
5330           (error "Couldn't activate group %s: %s"
5331                  (gnus-group-decoded-name group) (gnus-status-message group))))
5332
5333     (unless (gnus-request-group group t)
5334       (when (equal major-mode 'gnus-summary-mode)
5335         (gnus-kill-buffer (current-buffer)))
5336       (error "Couldn't request group %s: %s"
5337              (gnus-group-decoded-name group) (gnus-status-message group)))
5338
5339     (when gnus-agent
5340       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5341
5342       (setq gnus-summary-use-undownloaded-faces
5343             (gnus-agent-find-parameter
5344              group
5345              'agent-enable-undownloaded-faces)))
5346
5347     (setq gnus-newsgroup-name group
5348           gnus-newsgroup-unselected nil
5349           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5350
5351     (let ((display (gnus-group-find-parameter group 'display)))
5352       (setq gnus-newsgroup-display
5353             (cond
5354              ((not (zerop (or (car-safe read-all) 0)))
5355               ;; The user entered the group with C-u SPC/RET, let's show
5356               ;; all articles.
5357               'gnus-not-ignore)
5358              ((eq display 'all)
5359               'gnus-not-ignore)
5360              ((arrayp display)
5361               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5362              ((numberp display)
5363               ;; The following is probably the "correct" solution, but
5364               ;; it makes Gnus fetch all headers and then limit the
5365               ;; articles (which is slow), so instead we hack the
5366               ;; select-articles parameter instead. -- Simon Josefsson
5367               ;; <jas@kth.se>
5368               ;;
5369               ;; (gnus-byte-compile
5370               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5371               ;;                         display)))))
5372               (setq select-articles
5373                     (gnus-uncompress-range
5374                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5375                              (if (> tmp 0)
5376                                  tmp
5377                                1))
5378                            (cdr (gnus-active group)))))
5379               nil)
5380              (t
5381               nil))))
5382
5383     (gnus-summary-setup-default-charset)
5384
5385     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5386     (when (gnus-virtual-group-p group)
5387       (setq cached gnus-newsgroup-cached))
5388
5389     (setq gnus-newsgroup-unreads
5390           (gnus-sorted-ndifference
5391            (gnus-sorted-ndifference gnus-newsgroup-unreads
5392                                     gnus-newsgroup-marked)
5393            gnus-newsgroup-dormant))
5394
5395     (setq gnus-newsgroup-processable nil)
5396
5397     (gnus-update-read-articles group gnus-newsgroup-unreads)
5398
5399     ;; Adjust and set lists of article marks.
5400     (when info
5401       (gnus-adjust-marked-articles info))
5402     (if (setq articles select-articles)
5403         (setq gnus-newsgroup-unselected
5404               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5405       (setq articles (gnus-articles-to-read group read-all)))
5406
5407     (cond
5408      ((null articles)
5409       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5410       'quit)
5411      ((eq articles 0) nil)
5412      (t
5413       ;; Init the dependencies hash table.
5414       (setq gnus-newsgroup-dependencies
5415             (gnus-make-hashtable (length articles)))
5416       (gnus-set-global-variables)
5417       ;; Retrieve the headers and read them in.
5418
5419       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5420
5421       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5422       (when cached
5423         (setq gnus-newsgroup-cached cached))
5424
5425       ;; Suppress duplicates?
5426       (when gnus-suppress-duplicates
5427         (gnus-dup-suppress-articles))
5428
5429       ;; Set the initial limit.
5430       (setq gnus-newsgroup-limit (copy-sequence articles))
5431       ;; Remove canceled articles from the list of unread articles.
5432       (setq fetched-articles
5433             (mapcar (lambda (headers) (mail-header-number headers))
5434                     gnus-newsgroup-headers))
5435       (setq gnus-newsgroup-articles fetched-articles)
5436       (setq gnus-newsgroup-unreads
5437             (gnus-sorted-nintersection
5438              gnus-newsgroup-unreads fetched-articles))
5439       (gnus-compute-unseen-list)
5440
5441       ;; Removed marked articles that do not exist.
5442       (gnus-update-missing-marks
5443        (gnus-sorted-difference articles fetched-articles))
5444       ;; We might want to build some more threads first.
5445       (when (and gnus-fetch-old-headers
5446                  (eq gnus-headers-retrieved-by 'nov))
5447         (if (eq gnus-fetch-old-headers 'invisible)
5448             (gnus-build-all-threads)
5449           (gnus-build-old-threads)))
5450       ;; Let the Gnus agent mark articles as read.
5451       (when gnus-agent
5452         (gnus-agent-get-undownloaded-list))
5453       ;; Remove list identifiers from subject
5454       (when gnus-list-identifiers
5455         (gnus-summary-remove-list-identifiers))
5456       ;; Check whether auto-expire is to be done in this group.
5457       (setq gnus-newsgroup-auto-expire
5458             (gnus-group-auto-expirable-p group))
5459       ;; Set up the article buffer now, if necessary.
5460       (unless (and gnus-single-article-buffer
5461                    (equal gnus-article-buffer "*Article*"))
5462         (gnus-article-setup-buffer))
5463       ;; First and last article in this newsgroup.
5464       (when gnus-newsgroup-headers
5465         (setq gnus-newsgroup-begin
5466               (mail-header-number (car gnus-newsgroup-headers))
5467               gnus-newsgroup-end
5468               (mail-header-number
5469                (gnus-last-element gnus-newsgroup-headers))))
5470       ;; GROUP is successfully selected.
5471       (or gnus-newsgroup-headers t)))))
5472
5473 (defun gnus-compute-unseen-list ()
5474   ;; The `seen' marks are treated specially.
5475   (if (not gnus-newsgroup-seen)
5476       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5477     (setq gnus-newsgroup-unseen
5478           (gnus-inverse-list-range-intersection
5479            gnus-newsgroup-articles gnus-newsgroup-seen))))
5480
5481 (defun gnus-summary-display-make-predicate (display)
5482   (require 'gnus-agent)
5483   (when (= (length display) 1)
5484     (setq display (car display)))
5485   (unless gnus-summary-display-cache
5486     (dolist (elem (append '((unread . unread)
5487                             (read . read)
5488                             (unseen . unseen))
5489                           gnus-article-mark-lists))
5490       (push (cons (cdr elem)
5491                   (gnus-byte-compile
5492                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5493             gnus-summary-display-cache)))
5494   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5495         (gnus-category-predicate-cache gnus-summary-display-cache))
5496     (gnus-get-predicate display)))
5497
5498 ;; Uses the dynamically bound `number' variable.
5499 (eval-when-compile
5500   (defvar number))
5501 (defun gnus-article-marked-p (type &optional article)
5502   (let ((article (or article number)))
5503     (cond
5504      ((eq type 'tick)
5505       (memq article gnus-newsgroup-marked))
5506      ((eq type 'spam)
5507       (memq article gnus-newsgroup-spam-marked))
5508      ((eq type 'unsend)
5509       (memq article gnus-newsgroup-unsendable))
5510      ((eq type 'undownload)
5511       (memq article gnus-newsgroup-undownloaded))
5512      ((eq type 'download)
5513       (memq article gnus-newsgroup-downloadable))
5514      ((eq type 'unread)
5515       (memq article gnus-newsgroup-unreads))
5516      ((eq type 'read)
5517       (memq article gnus-newsgroup-reads))
5518      ((eq type 'dormant)
5519       (memq article gnus-newsgroup-dormant) )
5520      ((eq type 'expire)
5521       (memq article gnus-newsgroup-expirable))
5522      ((eq type 'reply)
5523       (memq article gnus-newsgroup-replied))
5524      ((eq type 'killed)
5525       (memq article gnus-newsgroup-killed))
5526      ((eq type 'bookmark)
5527       (assq article gnus-newsgroup-bookmarks))
5528      ((eq type 'score)
5529       (assq article gnus-newsgroup-scored))
5530      ((eq type 'save)
5531       (memq article gnus-newsgroup-saved))
5532      ((eq type 'cache)
5533       (memq article gnus-newsgroup-cached))
5534      ((eq type 'forward)
5535       (memq article gnus-newsgroup-forwarded))
5536      ((eq type 'seen)
5537       (not (memq article gnus-newsgroup-unseen)))
5538      ((eq type 'recent)
5539       (memq article gnus-newsgroup-recent))
5540      (t t))))
5541
5542 (defun gnus-articles-to-read (group &optional read-all)
5543   "Find out what articles the user wants to read."
5544   (let* ((articles
5545           ;; Select all articles if `read-all' is non-nil, or if there
5546           ;; are no unread articles.
5547           (if (or read-all
5548                   (and (zerop (length gnus-newsgroup-marked))
5549                        (zerop (length gnus-newsgroup-unreads)))
5550                   ;; Fetch all if the predicate is non-nil.
5551                   gnus-newsgroup-display)
5552               ;; We want to select the headers for all the articles in
5553               ;; the group, so we select either all the active
5554               ;; articles in the group, or (if that's nil), the
5555               ;; articles in the cache.
5556               (or
5557                (gnus-uncompress-range (gnus-active group))
5558                (gnus-cache-articles-in-group group))
5559             ;; Select only the "normal" subset of articles.
5560             (gnus-sorted-nunion
5561              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5562              gnus-newsgroup-unreads)))
5563          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5564          (scored (length scored-list))
5565          (number (length articles))
5566          (marked (+ (length gnus-newsgroup-marked)
5567                     (length gnus-newsgroup-dormant)))
5568          (select
5569           (cond
5570            ((numberp read-all)
5571             read-all)
5572            ((numberp gnus-newsgroup-display)
5573             gnus-newsgroup-display)
5574            (t
5575             (condition-case ()
5576                 (cond
5577                  ((and (or (<= scored marked) (= scored number))
5578                        (numberp gnus-large-newsgroup)
5579                        (> number gnus-large-newsgroup))
5580                   (let* ((cursor-in-echo-area nil)
5581                          (initial (gnus-parameter-large-newsgroup-initial
5582                                    gnus-newsgroup-name))
5583                          (input
5584                           (read-string
5585                            (format
5586                             "How many articles from %s (%s %d): "
5587                             (gnus-limit-string
5588                              (gnus-group-decoded-name gnus-newsgroup-name)
5589                              35)
5590                             (if initial "max" "default")
5591                             number)
5592                            (if initial
5593                                (cons (number-to-string initial)
5594                                      0)))))
5595                     (if (string-match "^[ \t]*$" input) number input)))
5596                  ((and (> scored marked) (< scored number)
5597                        (> (- scored number) 20))
5598                   (let ((input
5599                          (read-string
5600                           (format "%s %s (%d scored, %d total): "
5601                                   "How many articles from"
5602                                   (gnus-group-decoded-name group)
5603                                   scored number))))
5604                     (if (string-match "^[ \t]*$" input)
5605                         number input)))
5606                  (t number))
5607               (quit
5608                (message "Quit getting the articles to read")
5609                nil))))))
5610     (setq select (if (stringp select) (string-to-number select) select))
5611     (if (or (null select) (zerop select))
5612         select
5613       (if (and (not (zerop scored)) (<= (abs select) scored))
5614           (progn
5615             (setq articles (sort scored-list '<))
5616             (setq number (length articles)))
5617         (setq articles (copy-sequence articles)))
5618
5619       (when (< (abs select) number)
5620         (if (< select 0)
5621             ;; Select the N oldest articles.
5622             (setcdr (nthcdr (1- (abs select)) articles) nil)
5623           ;; Select the N most recent articles.
5624           (setq articles (nthcdr (- number select) articles))))
5625       (setq gnus-newsgroup-unselected
5626             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5627       (when gnus-alter-articles-to-read-function
5628         (setq articles
5629               (sort
5630                (funcall gnus-alter-articles-to-read-function
5631                         gnus-newsgroup-name articles)
5632                '<)))
5633       articles)))
5634
5635 (defun gnus-killed-articles (killed articles)
5636   (let (out)
5637     (while articles
5638       (when (inline (gnus-member-of-range (car articles) killed))
5639         (push (car articles) out))
5640       (setq articles (cdr articles)))
5641     out))
5642
5643 (defun gnus-uncompress-marks (marks)
5644   "Uncompress the mark ranges in MARKS."
5645   (let ((uncompressed '(score bookmark))
5646         out)
5647     (while marks
5648       (if (memq (caar marks) uncompressed)
5649           (push (car marks) out)
5650         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5651       (setq marks (cdr marks)))
5652     out))
5653
5654 (defun gnus-article-mark-to-type (mark)
5655   "Return the type of MARK."
5656   (or (cadr (assq mark gnus-article-special-mark-lists))
5657       'list))
5658
5659 (defun gnus-article-unpropagatable-p (mark)
5660   "Return whether MARK should be propagated to back end."
5661   (memq mark gnus-article-unpropagated-mark-lists))
5662
5663 (defun gnus-adjust-marked-articles (info)
5664   "Set all article lists and remove all marks that are no longer valid."
5665   (let* ((marked-lists (gnus-info-marks info))
5666          (active (gnus-active (gnus-info-group info)))
5667          (min (car active))
5668          (max (cdr active))
5669          (types gnus-article-mark-lists)
5670          marks var articles article mark mark-type
5671          bgn end)
5672
5673     (dolist (marks marked-lists)
5674       (setq mark (car marks)
5675             mark-type (gnus-article-mark-to-type mark)
5676             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5677
5678       ;; We set the variable according to the type of the marks list,
5679       ;; and then adjust the marks to a subset of the active articles.
5680       (cond
5681        ;; Adjust "simple" lists - compressed yet unsorted
5682        ((eq mark-type 'list)
5683         ;; Simultaneously uncompress and clip to active range
5684         ;; See gnus-uncompress-range for a description of possible marks
5685         (let (l lh)
5686           (if (not (cadr marks))
5687               (set var nil)
5688             (setq articles (if (numberp (cddr marks))
5689                                (list (cdr marks))
5690                              (cdr marks))
5691                   lh (cons nil nil)
5692                   l lh)
5693
5694             (while (setq article (pop articles))
5695               (cond ((consp article)
5696                      (setq bgn (max (car article) min)
5697                            end (min (cdr article) max))
5698                      (while (<= bgn end)
5699                        (setq l (setcdr l (cons bgn nil))
5700                              bgn (1+ bgn))))
5701                     ((and (<= min article)
5702                           (>= max article))
5703                      (setq l (setcdr l (cons article nil))))))
5704             (set var (cdr lh)))))
5705        ;; Adjust assocs.
5706        ((eq mark-type 'tuple)
5707         (set var (setq articles (cdr marks)))
5708         (when (not (listp (cdr (symbol-value var))))
5709           (set var (list (symbol-value var))))
5710         (when (not (listp (cdr articles)))
5711           (setq articles (list articles)))
5712         (while articles
5713           (when (or (not (consp (setq article (pop articles))))
5714                     (< (car article) min)
5715                     (> (car article) max))
5716             (set var (delq article (symbol-value var))))))
5717        ;; Adjust ranges (sloppily).
5718        ((eq mark-type 'range)
5719         (cond
5720          ((eq mark 'seen)
5721           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5722           ;; It should be (seen (NUM1 . NUM2)).
5723           (when (numberp (cddr marks))
5724             (setcdr marks (list (cdr marks))))
5725           (setq articles (cdr marks))
5726           (while (and articles
5727                       (or (and (consp (car articles))
5728                                (> min (cdar articles)))
5729                           (and (numberp (car articles))
5730                                (> min (car articles)))))
5731             (pop articles))
5732           (set var articles))))))))
5733
5734 (defun gnus-update-missing-marks (missing)
5735   "Go through the list of MISSING articles and remove them from the mark lists."
5736   (when missing
5737     (let (var m)
5738       ;; Go through all types.
5739       (dolist (elem gnus-article-mark-lists)
5740         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5741           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5742           (when (symbol-value var)
5743             ;; This list has articles.  So we delete all missing
5744             ;; articles from it.
5745             (setq m missing)
5746             (while m
5747               (set var (delq (pop m) (symbol-value var))))))))))
5748
5749 (defun gnus-update-marks ()
5750   "Enter the various lists of marked articles into the newsgroup info list."
5751   (let ((types gnus-article-mark-lists)
5752         (info (gnus-get-info gnus-newsgroup-name))
5753         type list newmarked symbol delta-marks)
5754     (when info
5755       ;; Add all marks lists to the list of marks lists.
5756       (while (setq type (pop types))
5757         (setq list (symbol-value
5758                     (setq symbol
5759                           (intern (format "gnus-newsgroup-%s" (car type))))))
5760
5761         (when list
5762           ;; Get rid of the entries of the articles that have the
5763           ;; default score.
5764           (when (and (eq (cdr type) 'score)
5765                      gnus-save-score
5766                      list)
5767             (let* ((arts list)
5768                    (prev (cons nil list))
5769                    (all prev))
5770               (while arts
5771                 (if (or (not (consp (car arts)))
5772                         (= (cdar arts) gnus-summary-default-score))
5773                     (setcdr prev (cdr arts))
5774                   (setq prev arts))
5775                 (setq arts (cdr arts)))
5776               (setq list (cdr all)))))
5777
5778         (when (eq (cdr type) 'seen)
5779           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5780
5781         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5782           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5783
5784         (when (and (gnus-check-backend-function
5785                     'request-set-mark gnus-newsgroup-name)
5786                    (not (gnus-article-unpropagatable-p (cdr type))))
5787           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5788                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5789                  (add (gnus-remove-from-range
5790                        (gnus-copy-sequence list) old)))
5791             (when add
5792               (push (list add 'add (list (cdr type))) delta-marks))
5793             (when del
5794               (push (list del 'del (list (cdr type))) delta-marks))))
5795
5796         (when list
5797           (push (cons (cdr type) list) newmarked)))
5798
5799       (when delta-marks
5800         (unless (gnus-check-group gnus-newsgroup-name)
5801           (error "Can't open server for %s" gnus-newsgroup-name))
5802         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5803
5804       ;; Enter these new marks into the info of the group.
5805       (if (nthcdr 3 info)
5806           (setcar (nthcdr 3 info) newmarked)
5807         ;; Add the marks lists to the end of the info.
5808         (when newmarked
5809           (setcdr (nthcdr 2 info) (list newmarked))))
5810
5811       ;; Cut off the end of the info if there's nothing else there.
5812       (let ((i 5))
5813         (while (and (> i 2)
5814                     (not (nth i info)))
5815           (when (nthcdr (decf i) info)
5816             (setcdr (nthcdr i info) nil)))))))
5817
5818 (defun gnus-set-mode-line (where)
5819   "Set the mode line of the article or summary buffers.
5820 If WHERE is `summary', the summary mode line format will be used."
5821   ;; Is this mode line one we keep updated?
5822   (when (and (memq where gnus-updated-mode-lines)
5823              (symbol-value
5824               (intern (format "gnus-%s-mode-line-format-spec" where))))
5825     (let (mode-string)
5826       (save-excursion
5827         ;; We evaluate this in the summary buffer since these
5828         ;; variables are buffer-local to that buffer.
5829         (set-buffer gnus-summary-buffer)
5830        ;; We bind all these variables that are used in the `eval' form
5831         ;; below.
5832         (let* ((mformat (symbol-value
5833                          (intern
5834                           (format "gnus-%s-mode-line-format-spec" where))))
5835                (gnus-tmp-group-name (gnus-group-decoded-name
5836                                      gnus-newsgroup-name))
5837                (gnus-tmp-article-number (or gnus-current-article 0))
5838                (gnus-tmp-unread gnus-newsgroup-unreads)
5839                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5840                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5841                (gnus-tmp-unread-and-unselected
5842                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5843                             (zerop gnus-tmp-unselected))
5844                        "")
5845                       ((zerop gnus-tmp-unselected)
5846                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5847                       (t (format "{%d(+%d) more}"
5848                                  gnus-tmp-unread-and-unticked
5849                                  gnus-tmp-unselected))))
5850                (gnus-tmp-subject
5851                 (if (and gnus-current-headers
5852                          (vectorp gnus-current-headers))
5853                     (gnus-mode-string-quote
5854                      (mail-header-subject gnus-current-headers))
5855                   ""))
5856                bufname-length max-len
5857                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5858           (setq mode-string (eval mformat))
5859           (setq bufname-length (if (string-match "%b" mode-string)
5860                                    (- (length
5861                                        (buffer-name
5862                                         (if (eq where 'summary)
5863                                             nil
5864                                           (get-buffer gnus-article-buffer))))
5865                                       2)
5866                                  0))
5867           (setq max-len (max 4 (if gnus-mode-non-string-length
5868                                    (- (window-width)
5869                                       gnus-mode-non-string-length
5870                                       bufname-length)
5871                                  (length mode-string))))
5872           ;; We might have to chop a bit of the string off...
5873           (when (> (length mode-string) max-len)
5874             (setq mode-string
5875                   (concat (truncate-string-to-width mode-string (- max-len 3))
5876                           "...")))
5877           ;; Pad the mode string a bit.
5878           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5879       ;; Update the mode line.
5880       (setq mode-line-buffer-identification
5881             (gnus-mode-line-buffer-identification (list mode-string)))
5882       (set-buffer-modified-p t))))
5883
5884 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5885   "Go through the HEADERS list and add all Xrefs to a hash table.
5886 The resulting hash table is returned, or nil if no Xrefs were found."
5887   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5888          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5889          (xref-hashtb (gnus-make-hashtable))
5890          start group entry number xrefs header)
5891     (while headers
5892       (setq header (pop headers))
5893       (when (and (setq xrefs (mail-header-xref header))
5894                  (not (memq (setq number (mail-header-number header))
5895                             unreads)))
5896         (setq start 0)
5897         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5898           (setq start (match-end 0))
5899           (setq group (if prefix
5900                           (concat prefix (substring xrefs (match-beginning 1)
5901                                                     (match-end 1)))
5902                         (substring xrefs (match-beginning 1) (match-end 1))))
5903           (setq number
5904                 (string-to-number (substring xrefs (match-beginning 2)
5905                                           (match-end 2))))
5906           (if (setq entry (gnus-gethash group xref-hashtb))
5907               (setcdr entry (cons number (cdr entry)))
5908             (gnus-sethash group (cons number nil) xref-hashtb)))))
5909     (and start xref-hashtb)))
5910
5911 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5912   "Look through all the headers and mark the Xrefs as read."
5913   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5914         name info xref-hashtb idlist method nth4)
5915     (save-excursion
5916       (set-buffer gnus-group-buffer)
5917       (when (setq xref-hashtb
5918                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5919         (mapatoms
5920          (lambda (group)
5921            (unless (string= from-newsgroup (setq name (symbol-name group)))
5922              (setq idlist (symbol-value group))
5923              ;; Dead groups are not updated.
5924              (and (prog1
5925                       (setq info (gnus-get-info name))
5926                     (when (stringp (setq nth4 (gnus-info-method info)))
5927                       (setq nth4 (gnus-server-to-method nth4))))
5928                   ;; Only do the xrefs if the group has the same
5929                   ;; select method as the group we have just read.
5930                   (or (gnus-methods-equal-p
5931                        nth4 (gnus-find-method-for-group from-newsgroup))
5932                       virtual
5933                       (equal nth4 (setq method (gnus-find-method-for-group
5934                                                 from-newsgroup)))
5935                       (and (equal (car nth4) (car method))
5936                            (equal (nth 1 nth4) (nth 1 method))))
5937                   gnus-use-cross-reference
5938                   (or (not (eq gnus-use-cross-reference t))
5939                       virtual
5940                       ;; Only do cross-references on subscribed
5941                       ;; groups, if that is what is wanted.
5942                       (<= (gnus-info-level info) gnus-level-subscribed))
5943                   (gnus-group-make-articles-read name idlist))))
5944          xref-hashtb)))))
5945
5946 (defun gnus-compute-read-articles (group articles)
5947   (let* ((entry (gnus-group-entry group))
5948          (info (nth 2 entry))
5949          (active (gnus-active group))
5950          ninfo)
5951     (when entry
5952       ;; First peel off all invalid article numbers.
5953       (when active
5954         (let ((ids articles)
5955               id first)
5956           (while (setq id (pop ids))
5957             (when (and first (> id (cdr active)))
5958               ;; We'll end up in this situation in one particular
5959               ;; obscure situation.  If you re-scan a group and get
5960               ;; a new article that is cross-posted to a different
5961               ;; group that has not been re-scanned, you might get
5962               ;; crossposted article that has a higher number than
5963               ;; Gnus believes possible.  So we re-activate this
5964               ;; group as well.  This might mean doing the
5965               ;; crossposting thingy will *increase* the number
5966               ;; of articles in some groups.  Tsk, tsk.
5967               (setq active (or (gnus-activate-group group) active)))
5968             (when (or (> id (cdr active))
5969                       (< id (car active)))
5970               (setq articles (delq id articles))))))
5971       ;; If the read list is nil, we init it.
5972       (if (and active
5973                (null (gnus-info-read info))
5974                (> (car active) 1))
5975           (setq ninfo (cons 1 (1- (car active))))
5976         (setq ninfo (gnus-info-read info)))
5977       ;; Then we add the read articles to the range.
5978       (gnus-add-to-range
5979        ninfo (setq articles (sort articles '<))))))
5980
5981 (defun gnus-group-make-articles-read (group articles)
5982   "Update the info of GROUP to say that ARTICLES are read."
5983   (let* ((num 0)
5984          (entry (gnus-group-entry group))
5985          (info (nth 2 entry))
5986          (active (gnus-active group))
5987          range)
5988     (when entry
5989       (setq range (gnus-compute-read-articles group articles))
5990       (with-current-buffer gnus-group-buffer
5991         (gnus-undo-register
5992           `(progn
5993              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5994              (gnus-info-set-read ',info ',(gnus-info-read info))
5995              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5996              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5997              (gnus-group-update-group ,group t))))
5998       ;; Add the read articles to the range.
5999       (gnus-info-set-read info range)
6000       (gnus-request-set-mark group (list (list range 'add '(read))))
6001       ;; Then we have to re-compute how many unread
6002       ;; articles there are in this group.
6003       (when active
6004         (cond
6005          ((not range)
6006           (setq num (- (1+ (cdr active)) (car active))))
6007          ((not (listp (cdr range)))
6008           (setq num (- (cdr active) (- (1+ (cdr range))
6009                                        (car range)))))
6010          (t
6011           (while range
6012             (if (numberp (car range))
6013                 (setq num (1+ num))
6014               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6015             (setq range (cdr range)))
6016           (setq num (- (cdr active) num))))
6017         ;; Update the number of unread articles.
6018         (setcar entry num)
6019         ;; Update the group buffer.
6020         (unless (gnus-ephemeral-group-p group)
6021           (gnus-group-update-group group t))))))
6022
6023 (defvar gnus-newsgroup-none-id 0)
6024
6025 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6026   (let ((cur nntp-server-buffer)
6027         (dependencies
6028          (or dependencies
6029              (with-current-buffer gnus-summary-buffer
6030                gnus-newsgroup-dependencies)))
6031         headers id end ref number
6032         (mail-parse-charset gnus-newsgroup-charset)
6033         (mail-parse-ignored-charsets
6034          (save-excursion (condition-case nil
6035                              (set-buffer gnus-summary-buffer)
6036                            (error))
6037                          gnus-newsgroup-ignored-charsets)))
6038     (save-excursion
6039       (set-buffer nntp-server-buffer)
6040       ;; Translate all TAB characters into SPACE characters.
6041       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6042       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6043       (ietf-drums-unfold-fws)
6044       (gnus-run-hooks 'gnus-parse-headers-hook)
6045       (let ((case-fold-search t)
6046             in-reply-to header p lines chars)
6047         (goto-char (point-min))
6048         ;; Search to the beginning of the next header.  Error messages
6049         ;; do not begin with 2 or 3.
6050         (while (re-search-forward "^[23][0-9]+ " nil t)
6051           (setq id nil
6052                 ref nil)
6053           ;; This implementation of this function, with nine
6054           ;; search-forwards instead of the one re-search-forward and
6055           ;; a case (which basically was the old function) is actually
6056           ;; about twice as fast, even though it looks messier.  You
6057           ;; can't have everything, I guess.  Speed and elegance
6058           ;; doesn't always go hand in hand.
6059           (setq
6060            header
6061            (vector
6062             ;; Number.
6063             (prog1
6064                 (setq number (read cur))
6065               (end-of-line)
6066               (setq p (point))
6067               (narrow-to-region (point)
6068                                 (or (and (search-forward "\n.\n" nil t)
6069                                          (- (point) 2))
6070                                     (point))))
6071             ;; Subject.
6072             (progn
6073               (goto-char p)
6074               (if (search-forward "\nsubject:" nil t)
6075                   (funcall gnus-decode-encoded-word-function
6076                            (nnheader-header-value))
6077                 "(none)"))
6078             ;; From.
6079             (progn
6080               (goto-char p)
6081               (if (search-forward "\nfrom:" nil t)
6082                   (funcall gnus-decode-encoded-address-function
6083                            (nnheader-header-value))
6084                 "(nobody)"))
6085             ;; Date.
6086             (progn
6087               (goto-char p)
6088               (if (search-forward "\ndate:" nil t)
6089                   (nnheader-header-value) ""))
6090             ;; Message-ID.
6091             (progn
6092               (goto-char p)
6093               (setq id (if (re-search-forward
6094                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6095                            ;; We do it this way to make sure the Message-ID
6096                            ;; is (somewhat) syntactically valid.
6097                            (buffer-substring (match-beginning 1)
6098                                              (match-end 1))
6099                          ;; If there was no message-id, we just fake one
6100                          ;; to make subsequent routines simpler.
6101                          (nnheader-generate-fake-message-id number))))
6102             ;; References.
6103             (progn
6104               (goto-char p)
6105               (if (search-forward "\nreferences:" nil t)
6106                   (progn
6107                     (setq end (point))
6108                     (prog1
6109                         (nnheader-header-value)
6110                       (setq ref
6111                             (buffer-substring
6112                              (progn
6113                                (end-of-line)
6114                                (search-backward ">" end t)
6115                                (1+ (point)))
6116                              (progn
6117                                (search-backward "<" end t)
6118                                (point))))))
6119                 ;; Get the references from the in-reply-to header if there
6120                 ;; were no references and the in-reply-to header looks
6121                 ;; promising.
6122                 (if (and (search-forward "\nin-reply-to:" nil t)
6123                          (setq in-reply-to (nnheader-header-value))
6124                          (string-match "<[^>]+>" in-reply-to))
6125                     (let (ref2)
6126                       (setq ref (substring in-reply-to (match-beginning 0)
6127                                            (match-end 0)))
6128                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6129                         (setq ref2 (substring in-reply-to (match-beginning 0)
6130                                               (match-end 0)))
6131                         (when (> (length ref2) (length ref))
6132                           (setq ref ref2)))
6133                       ref)
6134                   (setq ref nil))))
6135             ;; Chars.
6136             (progn
6137               (goto-char p)
6138               (if (search-forward "\nchars: " nil t)
6139                   (if (numberp (setq chars (ignore-errors (read cur))))
6140                       chars -1)
6141                 -1))
6142             ;; Lines.
6143             (progn
6144               (goto-char p)
6145               (if (search-forward "\nlines: " nil t)
6146                   (if (numberp (setq lines (ignore-errors (read cur))))
6147                       lines -1)
6148                 -1))
6149             ;; Xref.
6150             (progn
6151               (goto-char p)
6152               (and (search-forward "\nxref:" nil t)
6153                    (nnheader-header-value)))
6154             ;; Extra.
6155             (when gnus-extra-headers
6156               (let ((extra gnus-extra-headers)
6157                     out)
6158                 (while extra
6159                   (goto-char p)
6160                   (when (search-forward
6161                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6162                     (push (cons (car extra) (nnheader-header-value))
6163                           out))
6164                   (pop extra))
6165                 out))))
6166           (when (equal id ref)
6167             (setq ref nil))
6168
6169           (when gnus-alter-header-function
6170             (funcall gnus-alter-header-function header)
6171             (setq id (mail-header-id header)
6172                   ref (gnus-parent-id (mail-header-references header))))
6173
6174           (when (setq header
6175                       (gnus-dependencies-add-header
6176                        header dependencies force-new))
6177             (push header headers))
6178           (goto-char (point-max))
6179           (widen))
6180         (nreverse headers)))))
6181
6182 ;; Goes through the xover lines and returns a list of vectors
6183 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6184                                                   force-new dependencies
6185                                                   group also-fetch-heads)
6186   "Parse the news overview data in the server buffer.
6187 Return a list of headers that match SEQUENCE (see
6188 `nntp-retrieve-headers')."
6189   ;; Get the Xref when the users reads the articles since most/some
6190   ;; NNTP servers do not include Xrefs when using XOVER.
6191   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6192   (let ((mail-parse-charset gnus-newsgroup-charset)
6193         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6194         (cur nntp-server-buffer)
6195         (dependencies (or dependencies gnus-newsgroup-dependencies))
6196         (allp (cond
6197                ((eq gnus-read-all-available-headers t)
6198                 t)
6199                ((and (stringp gnus-read-all-available-headers)
6200                      group)
6201                 (string-match gnus-read-all-available-headers group))
6202                (t
6203                 nil)))
6204         number headers header)
6205     (save-excursion
6206       (set-buffer nntp-server-buffer)
6207       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6208       ;; Allow the user to mangle the headers before parsing them.
6209       (gnus-run-hooks 'gnus-parse-headers-hook)
6210       (goto-char (point-min))
6211       (gnus-parse-without-error
6212         (while (and (or sequence allp)
6213                     (not (eobp)))
6214           (setq number (read cur))
6215           (when (not allp)
6216             (while (and sequence
6217                         (< (car sequence) number))
6218               (setq sequence (cdr sequence))))
6219           (when (and (or allp
6220                          (and sequence
6221                               (eq number (car sequence))))
6222                      (progn
6223                        (setq sequence (cdr sequence))
6224                        (setq header (inline
6225                                       (gnus-nov-parse-line
6226                                        number dependencies force-new)))))
6227             (push header headers))
6228           (forward-line 1)))
6229       ;; A common bug in inn is that if you have posted an article and
6230       ;; then retrieves the active file, it will answer correctly --
6231       ;; the new article is included.  However, a NOV entry for the
6232       ;; article may not have been generated yet, so this may fail.
6233       ;; We work around this problem by retrieving the last few
6234       ;; headers using HEAD.
6235       (if (or (not also-fetch-heads)
6236               (not sequence))
6237           ;; We (probably) got all the headers.
6238           (nreverse headers)
6239         (let ((gnus-nov-is-evil t))
6240           (nconc
6241            (nreverse headers)
6242            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6243              (gnus-get-newsgroup-headers))))))))
6244
6245 (defun gnus-article-get-xrefs ()
6246   "Fill in the Xref value in `gnus-current-headers', if necessary.
6247 This is meant to be called in `gnus-article-internal-prepare-hook'."
6248   (let ((headers (with-current-buffer gnus-summary-buffer
6249                    gnus-current-headers)))
6250     (or (not gnus-use-cross-reference)
6251         (not headers)
6252         (and (mail-header-xref headers)
6253              (not (string= (mail-header-xref headers) "")))
6254         (let ((case-fold-search t)
6255               xref)
6256           (save-restriction
6257             (nnheader-narrow-to-headers)
6258             (goto-char (point-min))
6259             (when (or (and (not (eobp))
6260                            (eq (downcase (char-after)) ?x)
6261                            (looking-at "Xref:"))
6262                       (search-forward "\nXref:" nil t))
6263               (goto-char (1+ (match-end 0)))
6264               (setq xref (buffer-substring (point) (point-at-eol)))
6265               (mail-header-set-xref headers xref)))))))
6266
6267 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6268   "Find article ID and insert the summary line for that article.
6269 OLD-HEADER can either be a header or a line number to insert
6270 the subject line on."
6271   (let* ((line (and (numberp old-header) old-header))
6272          (old-header (and (vectorp old-header) old-header))
6273          (header (cond ((and old-header use-old-header)
6274                         old-header)
6275                        ((and (numberp id)
6276                              (gnus-number-to-header id))
6277                         (gnus-number-to-header id))
6278                        (t
6279                         (gnus-read-header id))))
6280          (number (and (numberp id) id))
6281          d)
6282     (when header
6283       ;; Rebuild the thread that this article is part of and go to the
6284       ;; article we have fetched.
6285       (when (and (not gnus-show-threads)
6286                  old-header)
6287         (when (and number
6288                    (setq d (gnus-data-find (mail-header-number old-header))))
6289           (goto-char (gnus-data-pos d))
6290           (gnus-data-remove
6291            number
6292            (- (point-at-bol)
6293               (prog1
6294                   (1+ (point-at-eol))
6295                 (gnus-delete-line))))))
6296       ;; Remove list identifiers from subject.
6297       (when gnus-list-identifiers
6298         (let ((gnus-newsgroup-headers (list header)))
6299           (gnus-summary-remove-list-identifiers)))
6300       (when old-header
6301         (mail-header-set-number header (mail-header-number old-header)))
6302       (setq gnus-newsgroup-sparse
6303             (delq (setq number (mail-header-number header))
6304                   gnus-newsgroup-sparse))
6305       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6306       (push number gnus-newsgroup-limit)
6307       (gnus-rebuild-thread (mail-header-id header) line)
6308       (gnus-summary-goto-subject number nil t))
6309     (when (and (numberp number)
6310                (> number 0))
6311       ;; We have to update the boundaries even if we can't fetch the
6312       ;; article if ID is a number -- so that the next `P' or `N'
6313       ;; command will fetch the previous (or next) article even
6314       ;; if the one we tried to fetch this time has been canceled.
6315       (when (> number gnus-newsgroup-end)
6316         (setq gnus-newsgroup-end number))
6317       (when (< number gnus-newsgroup-begin)
6318         (setq gnus-newsgroup-begin number))
6319       (setq gnus-newsgroup-unselected
6320             (delq number gnus-newsgroup-unselected)))
6321     ;; Report back a success?
6322     (and header (mail-header-number header))))
6323
6324 ;;; Process/prefix in the summary buffer
6325
6326 (defun gnus-summary-work-articles (n)
6327   "Return a list of articles to be worked upon.
6328 The prefix argument, the list of process marked articles, and the
6329 current article will be taken into consideration."
6330   (save-excursion
6331     (set-buffer gnus-summary-buffer)
6332     (cond
6333      (n
6334       ;; A numerical prefix has been given.
6335       (setq n (prefix-numeric-value n))
6336       (let ((backward (< n 0))
6337             (n (abs (prefix-numeric-value n)))
6338             articles article)
6339         (save-excursion
6340           (while
6341               (and (> n 0)
6342                    (push (setq article (gnus-summary-article-number))
6343                          articles)
6344                    (if backward
6345                        (gnus-summary-find-prev nil article)
6346                      (gnus-summary-find-next nil article)))
6347             (decf n)))
6348         (nreverse articles)))
6349      ((and (gnus-region-active-p) (mark))
6350       (message "region active")
6351       ;; Work on the region between point and mark.
6352       (let ((max (max (point) (mark)))
6353             articles article)
6354         (save-excursion
6355           (goto-char (min (point) (mark)))
6356           (while
6357               (and
6358                (push (setq article (gnus-summary-article-number)) articles)
6359                (gnus-summary-find-next nil article)
6360                (< (point) max)))
6361           (nreverse articles))))
6362      (gnus-newsgroup-processable
6363       ;; There are process-marked articles present.
6364       ;; Save current state.
6365       (gnus-summary-save-process-mark)
6366       ;; Return the list.
6367       (reverse gnus-newsgroup-processable))
6368      (t
6369       ;; Just return the current article.
6370       (list (gnus-summary-article-number))))))
6371
6372 (defmacro gnus-summary-iterate (arg &rest forms)
6373   "Iterate over the process/prefixed articles and do FORMS.
6374 ARG is the interactive prefix given to the command.  FORMS will be
6375 executed with point over the summary line of the articles."
6376   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6377     `(let ((,articles (gnus-summary-work-articles ,arg)))
6378        (while ,articles
6379          (gnus-summary-goto-subject (car ,articles))
6380          ,@forms
6381          (pop ,articles)))))
6382
6383 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6384 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6385
6386 (defun gnus-summary-save-process-mark ()
6387   "Push the current set of process marked articles on the stack."
6388   (interactive)
6389   (push (copy-sequence gnus-newsgroup-processable)
6390         gnus-newsgroup-process-stack))
6391
6392 (defun gnus-summary-kill-process-mark ()
6393   "Push the current set of process marked articles on the stack and unmark."
6394   (interactive)
6395   (gnus-summary-save-process-mark)
6396   (gnus-summary-unmark-all-processable))
6397
6398 (defun gnus-summary-yank-process-mark ()
6399   "Pop the last process mark state off the stack and restore it."
6400   (interactive)
6401   (unless gnus-newsgroup-process-stack
6402     (error "Empty mark stack"))
6403   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6404
6405 (defun gnus-summary-process-mark-set (set)
6406   "Make SET into the current process marked articles."
6407   (gnus-summary-unmark-all-processable)
6408   (mapc 'gnus-summary-set-process-mark set))
6409
6410 ;;; Searching and stuff
6411
6412 (defun gnus-summary-search-group (&optional backward use-level)
6413   "Search for next unread newsgroup.
6414 If optional argument BACKWARD is non-nil, search backward instead."
6415   (save-excursion
6416     (set-buffer gnus-group-buffer)
6417     (when (gnus-group-search-forward
6418            backward nil (if use-level (gnus-group-group-level) nil))
6419       (gnus-group-group-name))))
6420
6421 (defun gnus-summary-best-group (&optional exclude-group)
6422   "Find the name of the best unread group.
6423 If EXCLUDE-GROUP, do not go to this group."
6424   (with-current-buffer gnus-group-buffer
6425     (save-excursion
6426       (gnus-group-best-unread-group exclude-group))))
6427
6428 (defun gnus-summary-find-next (&optional unread article backward)
6429   (if backward
6430       (gnus-summary-find-prev unread article)
6431     (let* ((dummy (gnus-summary-article-intangible-p))
6432            (article (or article (gnus-summary-article-number)))
6433            (data (gnus-data-find-list article))
6434            result)
6435       (when (and (not dummy)
6436                  (or (not gnus-summary-check-current)
6437                      (not unread)
6438                      (not (gnus-data-unread-p (car data)))))
6439         (setq data (cdr data)))
6440       (when (setq result
6441                   (if unread
6442                       (progn
6443                         (while data
6444                           (unless (memq (gnus-data-number (car data))
6445                                         (cond
6446                                          ((eq gnus-auto-goto-ignores
6447                                               'always-undownloaded)
6448                                           gnus-newsgroup-undownloaded)
6449                                          (gnus-plugged
6450                                           nil)
6451                                          ((eq gnus-auto-goto-ignores
6452                                               'unfetched)
6453                                           gnus-newsgroup-unfetched)
6454                                          ((eq gnus-auto-goto-ignores
6455                                               'undownloaded)
6456                                           gnus-newsgroup-undownloaded)))
6457                             (when (gnus-data-unread-p (car data))
6458                               (setq result (car data)
6459                                     data nil)))
6460                           (setq data (cdr data)))
6461                         result)
6462                     (car data)))
6463         (goto-char (gnus-data-pos result))
6464         (gnus-data-number result)))))
6465
6466 (defun gnus-summary-find-prev (&optional unread article)
6467   (let* ((eobp (eobp))
6468          (article (or article (gnus-summary-article-number)))
6469          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6470          result)
6471     (when (and (not eobp)
6472                (or (not gnus-summary-check-current)
6473                    (not unread)
6474                    (not (gnus-data-unread-p (car data)))))
6475       (setq data (cdr data)))
6476     (when (setq result
6477                 (if unread
6478                     (progn
6479                       (while data
6480                         (unless (memq (gnus-data-number (car data))
6481                                       (cond
6482                                        ((eq gnus-auto-goto-ignores
6483                                             'always-undownloaded)
6484                                         gnus-newsgroup-undownloaded)
6485                                        (gnus-plugged
6486                                         nil)
6487                                        ((eq gnus-auto-goto-ignores
6488                                             'unfetched)
6489                                         gnus-newsgroup-unfetched)
6490                                        ((eq gnus-auto-goto-ignores
6491                                             'undownloaded)
6492                                         gnus-newsgroup-undownloaded)))
6493                           (when (gnus-data-unread-p (car data))
6494                             (setq result (car data)
6495                                   data nil)))
6496                         (setq data (cdr data)))
6497                       result)
6498                   (car data)))
6499       (goto-char (gnus-data-pos result))
6500       (gnus-data-number result))))
6501
6502 (defun gnus-summary-find-subject (subject &optional unread backward article)
6503   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6504          (article (or article (gnus-summary-article-number)))
6505          (articles (gnus-data-list backward))
6506          (arts (gnus-data-find-list article articles))
6507          result)
6508     (when (or (not gnus-summary-check-current)
6509               (not unread)
6510               (not (gnus-data-unread-p (car arts))))
6511       (setq arts (cdr arts)))
6512     (while arts
6513       (and (or (not unread)
6514                (gnus-data-unread-p (car arts)))
6515            (vectorp (gnus-data-header (car arts)))
6516            (gnus-subject-equal
6517             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6518            (setq result (car arts)
6519                  arts nil))
6520       (setq arts (cdr arts)))
6521     (and result
6522          (goto-char (gnus-data-pos result))
6523          (gnus-data-number result))))
6524
6525 (defun gnus-summary-search-forward (&optional unread subject backward)
6526   "Search forward for an article.
6527 If UNREAD, look for unread articles.  If SUBJECT, look for
6528 articles with that subject.  If BACKWARD, search backward instead."
6529   (cond (subject (gnus-summary-find-subject subject unread backward))
6530         (backward (gnus-summary-find-prev unread))
6531         (t (gnus-summary-find-next unread))))
6532
6533 (defun gnus-recenter (&optional n)
6534   "Center point in window and redisplay frame.
6535 Also do horizontal recentering."
6536   (interactive "P")
6537   (when (and gnus-auto-center-summary
6538              (not (eq gnus-auto-center-summary 'vertical)))
6539     (gnus-horizontal-recenter))
6540   (recenter n))
6541
6542 (defun gnus-summary-recenter ()
6543   "Center point in the summary window.
6544 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6545 displayed, no centering will be performed."
6546   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6547   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6548   (interactive)
6549   ;; The user has to want it.
6550   (when gnus-auto-center-summary
6551     (let* ((top (cond ((< (window-height) 4) 0)
6552                       ((< (window-height) 7) 1)
6553                       (t (if (numberp gnus-auto-center-summary)
6554                              gnus-auto-center-summary
6555                            (/ (1- (window-height)) 2)))))
6556            (height (1- (window-height)))
6557            (bottom (save-excursion (goto-char (point-max))
6558                                    (forward-line (- height))
6559                                    (point)))
6560            (window (get-buffer-window (current-buffer))))
6561       (when (get-buffer-window gnus-article-buffer)
6562         ;; Only do recentering when the article buffer is displayed,
6563         ;; Set the window start to either `bottom', which is the biggest
6564         ;; possible valid number, or the second line from the top,
6565         ;; whichever is the least.
6566         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6567           (if (> bottom top-pos)
6568               ;; Keep the second line from the top visible
6569               (set-window-start window top-pos t)
6570             ;; Try to keep the bottom line visible; if it's partially
6571             ;; obscured, either scroll one more line to make it fully
6572             ;; visible, or revert to using TOP-POS.
6573             (save-excursion
6574               (goto-char (point-max))
6575               (forward-line -1)
6576               (let ((last-line-start (point)))
6577                 (goto-char bottom)
6578                 (set-window-start window (point) t)
6579                 (when (not (pos-visible-in-window-p last-line-start window))
6580                   (forward-line 1)
6581                   (set-window-start window (min (point) top-pos) t)))))))
6582       ;; Do horizontal recentering while we're at it.
6583       (when (and (get-buffer-window (current-buffer) t)
6584                  (not (eq gnus-auto-center-summary 'vertical)))
6585         (let ((selected (selected-window)))
6586           (select-window (get-buffer-window (current-buffer) t))
6587           (gnus-summary-position-point)
6588           (gnus-horizontal-recenter)
6589           (select-window selected))))))
6590
6591 (defun gnus-summary-jump-to-group (newsgroup)
6592   "Move point to NEWSGROUP in group mode buffer."
6593   ;; Keep update point of group mode buffer if visible.
6594   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6595       (save-window-excursion
6596         ;; Take care of tree window mode.
6597         (when (get-buffer-window gnus-group-buffer)
6598           (pop-to-buffer gnus-group-buffer))
6599         (gnus-group-jump-to-group newsgroup))
6600     (save-excursion
6601       ;; Take care of tree window mode.
6602       (if (get-buffer-window gnus-group-buffer)
6603           (pop-to-buffer gnus-group-buffer)
6604         (set-buffer gnus-group-buffer))
6605       (gnus-group-jump-to-group newsgroup))))
6606
6607 ;; This function returns a list of article numbers based on the
6608 ;; difference between the ranges of read articles in this group and
6609 ;; the range of active articles.
6610 (defun gnus-list-of-unread-articles (group)
6611   (let* ((read (gnus-info-read (gnus-get-info group)))
6612          (active (or (gnus-active group) (gnus-activate-group group)))
6613          (last (or (cdr active)
6614                    (error "Group %s couldn't be activated " group)))
6615          first nlast unread)
6616     ;; If none are read, then all are unread.
6617     (if (not read)
6618         (setq first (car active))
6619       ;; If the range of read articles is a single range, then the
6620       ;; first unread article is the article after the last read
6621       ;; article.  Sounds logical, doesn't it?
6622       (if (and (not (listp (cdr read)))
6623                (or (< (car read) (car active))
6624                    (progn (setq read (list read))
6625                           nil)))
6626           (setq first (max (car active) (1+ (cdr read))))
6627         ;; `read' is a list of ranges.
6628         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6629                                   (caar read)))
6630                   1)
6631           (setq first (car active)))
6632         (while read
6633           (when first
6634             (while (< first nlast)
6635               (setq unread (cons first unread)
6636                     first (1+ first))))
6637           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6638           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6639           (setq read (cdr read)))))
6640     ;; And add the last unread articles.
6641     (while (<= first last)
6642       (setq unread (cons first unread)
6643             first (1+ first)))
6644     ;; Return the list of unread articles.
6645     (delq 0 (nreverse unread))))
6646
6647 (defun gnus-list-of-read-articles (group)
6648   "Return a list of unread, unticked and non-dormant articles."
6649   (let* ((info (gnus-get-info group))
6650          (marked (gnus-info-marks info))
6651          (active (gnus-active group)))
6652     (and info active
6653          (gnus-list-range-difference
6654           (gnus-list-range-difference
6655            (gnus-sorted-complement
6656             (gnus-uncompress-range active)
6657             (gnus-list-of-unread-articles group))
6658            (cdr (assq 'dormant marked)))
6659           (cdr (assq 'tick marked))))))
6660
6661 ;; This function returns a sequence of article numbers based on the
6662 ;; difference between the ranges of read articles in this group and
6663 ;; the range of active articles.
6664 (defun gnus-sequence-of-unread-articles (group)
6665   (let* ((read (gnus-info-read (gnus-get-info group)))
6666          (active (or (gnus-active group) (gnus-activate-group group)))
6667          (last (cdr active))
6668          first nlast unread)
6669     ;; If none are read, then all are unread.
6670     (if (not read)
6671         (setq first (car active))
6672       ;; If the range of read articles is a single range, then the
6673       ;; first unread article is the article after the last read
6674       ;; article.  Sounds logical, doesn't it?
6675       (if (and (not (listp (cdr read)))
6676                (or (< (car read) (car active))
6677                    (progn (setq read (list read))
6678                           nil)))
6679           (setq first (max (car active) (1+ (cdr read))))
6680         ;; `read' is a list of ranges.
6681         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6682                                   (caar read)))
6683                   1)
6684           (setq first (car active)))
6685         (while read
6686           (when first
6687             (push (cons first nlast) unread))
6688           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6689           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6690           (setq read (cdr read)))))
6691     ;; And add the last unread articles.
6692     (cond ((not (and first last))
6693            nil)
6694           ((< first last)
6695            (push (cons first last) unread))
6696           ((= first last)
6697            (push first unread)))
6698     ;; Return the sequence of unread articles.
6699     (delq 0 (nreverse unread))))
6700
6701 ;; Various summary commands
6702
6703 (defun gnus-summary-select-article-buffer ()
6704   "Reconfigure windows to show article buffer."
6705   (interactive)
6706   (if (not (gnus-buffer-live-p gnus-article-buffer))
6707       (error "There is no article buffer for this summary buffer")
6708     (gnus-configure-windows 'article)
6709     (select-window (get-buffer-window gnus-article-buffer))))
6710
6711 (defun gnus-summary-universal-argument (arg)
6712   "Perform any operation on all articles that are process/prefixed."
6713   (interactive "P")
6714   (let ((articles (gnus-summary-work-articles arg))
6715         func article)
6716     (if (eq
6717          (setq
6718           func
6719           (key-binding
6720            (read-key-sequence
6721             (substitute-command-keys
6722              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6723          'undefined)
6724         (gnus-error 1 "Undefined key")
6725       (save-excursion
6726         (while articles
6727           (gnus-summary-goto-subject (setq article (pop articles)))
6728           (let (gnus-newsgroup-processable)
6729             (command-execute func))
6730           (gnus-summary-remove-process-mark article)))))
6731   (gnus-summary-position-point))
6732
6733 (defun gnus-summary-toggle-truncation (&optional arg)
6734   "Toggle truncation of summary lines.
6735 With ARG, turn line truncation on if ARG is positive."
6736   (interactive "P")
6737   (setq truncate-lines
6738         (if (null arg) (not truncate-lines)
6739           (> (prefix-numeric-value arg) 0)))
6740   (redraw-display))
6741
6742 (defun gnus-summary-find-for-reselect ()
6743   "Return the number of an article to stay on across a reselect.
6744 The current article is considered, then following articles, then previous
6745 articles.  An article is sought which is not cancelled and isn't a temporary
6746 insertion from another group.  If there's no such then return a dummy 0."
6747   (let (found)
6748     (dolist (rev '(nil t))
6749       (unless found      ; don't demand the reverse list if we don't need it
6750         (let ((data (gnus-data-find-list
6751                      (gnus-summary-article-number) (gnus-data-list rev))))
6752           (while (and data (not found))
6753             (if (and (< 0 (gnus-data-number (car data)))
6754                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6755                 (setq found (gnus-data-number (car data))))
6756             (setq data (cdr data))))))
6757     (or found 0)))
6758
6759 (defun gnus-summary-reselect-current-group (&optional all rescan)
6760   "Exit and then reselect the current newsgroup.
6761 The prefix argument ALL means to select all articles."
6762   (interactive "P")
6763   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6764     (error "Ephemeral groups can't be reselected"))
6765   (let ((current-subject (gnus-summary-find-for-reselect))
6766         (group gnus-newsgroup-name))
6767     (setq gnus-newsgroup-begin nil)
6768     (gnus-summary-exit nil 'leave-hidden)
6769     ;; We have to adjust the point of group mode buffer because
6770     ;; point was moved to the next unread newsgroup by exiting.
6771     (gnus-summary-jump-to-group group)
6772     (when rescan
6773       (save-excursion
6774         (gnus-group-get-new-news-this-group 1)))
6775     (gnus-group-read-group all t)
6776     (gnus-summary-goto-subject current-subject nil t)))
6777
6778 (defun gnus-summary-rescan-group (&optional all)
6779   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6780   (interactive "P")
6781   (gnus-summary-reselect-current-group all t))
6782
6783 (defun gnus-summary-update-info (&optional non-destructive)
6784   (save-excursion
6785     (let ((group gnus-newsgroup-name))
6786       (when group
6787         (when gnus-newsgroup-kill-headers
6788           (setq gnus-newsgroup-killed
6789                 (gnus-compress-sequence
6790                  (gnus-sorted-union
6791                   (gnus-list-range-intersection
6792                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6793                   gnus-newsgroup-unreads)
6794                  t)))
6795         (unless (listp (cdr gnus-newsgroup-killed))
6796           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6797         (let ((headers gnus-newsgroup-headers))
6798           ;; Set the new ranges of read articles.
6799           (with-current-buffer gnus-group-buffer
6800             (gnus-undo-force-boundary))
6801           (gnus-update-read-articles
6802            group (gnus-sorted-union
6803                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6804           ;; Set the current article marks.
6805           (let ((gnus-newsgroup-scored
6806                  (if (and (not gnus-save-score)
6807                           (not non-destructive))
6808                      nil
6809                    gnus-newsgroup-scored)))
6810             (save-excursion
6811               (gnus-update-marks)))
6812           ;; Do the cross-ref thing.
6813           (when gnus-use-cross-reference
6814             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6815           ;; Do not switch windows but change the buffer to work.
6816           (set-buffer gnus-group-buffer)
6817           (unless (gnus-ephemeral-group-p group)
6818             (gnus-group-update-group group)))))))
6819
6820 (defun gnus-summary-save-newsrc (&optional force)
6821   "Save the current number of read/marked articles in the dribble buffer.
6822 The dribble buffer will then be saved.
6823 If FORCE (the prefix), also save the .newsrc file(s)."
6824   (interactive "P")
6825   (gnus-summary-update-info t)
6826   (if force
6827       (gnus-save-newsrc-file)
6828     (gnus-dribble-save)))
6829
6830 (defun gnus-summary-exit (&optional temporary leave-hidden)
6831   "Exit reading current newsgroup, and then return to group selection mode.
6832 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6833   (interactive)
6834   (gnus-set-global-variables)
6835   (when (gnus-buffer-live-p gnus-article-buffer)
6836     (save-excursion
6837       (set-buffer gnus-article-buffer)
6838       (mm-destroy-parts gnus-article-mime-handles)
6839       ;; Set it to nil for safety reason.
6840       (setq gnus-article-mime-handle-alist nil)
6841       (setq gnus-article-mime-handles nil)))
6842   (gnus-kill-save-kill-buffer)
6843   (gnus-async-halt-prefetch)
6844   (let* ((group gnus-newsgroup-name)
6845          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6846          (gnus-group-is-exiting-p t)
6847          (mode major-mode)
6848          (group-point nil)
6849          (buf (current-buffer)))
6850     (unless quit-config
6851       ;; Do adaptive scoring, and possibly save score files.
6852       (when gnus-newsgroup-adaptive
6853         (gnus-score-adaptive))
6854       (when gnus-use-scoring
6855         (gnus-score-save)))
6856     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6857     ;; If we have several article buffers, we kill them at exit.
6858     (unless gnus-single-article-buffer
6859       (gnus-kill-buffer gnus-article-buffer)
6860       (gnus-kill-buffer gnus-original-article-buffer)
6861       (setq gnus-article-current nil))
6862     (when gnus-use-cache
6863       (gnus-cache-possibly-remove-articles)
6864       (gnus-cache-save-buffers))
6865     (gnus-async-prefetch-remove-group group)
6866     (when gnus-suppress-duplicates
6867       (gnus-dup-enter-articles))
6868     (when gnus-use-trees
6869       (gnus-tree-close group))
6870     (when gnus-use-cache
6871       (gnus-cache-write-active))
6872     ;; Remove entries for this group.
6873     (nnmail-purge-split-history (gnus-group-real-name group))
6874     ;; Make all changes in this group permanent.
6875     (unless quit-config
6876       (gnus-run-hooks 'gnus-exit-group-hook)
6877       (gnus-summary-update-info))
6878     (gnus-close-group group)
6879     ;; Make sure where we were, and go to next newsgroup.
6880     (set-buffer gnus-group-buffer)
6881     (unless quit-config
6882       (gnus-group-jump-to-group group))
6883     (gnus-run-hooks 'gnus-summary-exit-hook)
6884     (unless (or quit-config
6885                 ;; If this group has disappeared from the summary
6886                 ;; buffer, don't skip forwards.
6887                 (not (string= group (gnus-group-group-name))))
6888       (gnus-group-next-unread-group 1))
6889     (setq group-point (point))
6890     (if temporary
6891         nil                             ;Nothing to do.
6892       ;; If we have several article buffers, we kill them at exit.
6893       (unless gnus-single-article-buffer
6894         (gnus-kill-buffer gnus-article-buffer)
6895         (gnus-kill-buffer gnus-original-article-buffer)
6896         (setq gnus-article-current nil))
6897       (set-buffer buf)
6898       (if (not gnus-kill-summary-on-exit)
6899           (progn
6900             (gnus-deaden-summary)
6901             (setq mode nil))
6902         ;; We set all buffer-local variables to nil.  It is unclear why
6903         ;; this is needed, but if we don't, buffer-local variables are
6904         ;; not garbage-collected, it seems.  This would the lead to en
6905         ;; ever-growing Emacs.
6906         (gnus-summary-clear-local-variables)
6907         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6908           (gnus-summary-clear-local-variables))
6909         (when (get-buffer gnus-article-buffer)
6910           (bury-buffer gnus-article-buffer))
6911         ;; Return to group mode buffer.
6912         (when (eq mode 'gnus-summary-mode)
6913           (gnus-kill-buffer buf)))
6914       (setq gnus-current-select-method gnus-select-method)
6915       (set-buffer gnus-group-buffer)
6916       (if quit-config
6917           (gnus-handle-ephemeral-exit quit-config)
6918         (goto-char group-point)
6919         ;; If gnus-group-buffer is already displayed, make sure we also move
6920         ;; the cursor in the window that displays it.
6921         (let ((win (get-buffer-window (current-buffer) 0)))
6922           (if win (set-window-point win (point))))
6923         (unless leave-hidden
6924           (gnus-configure-windows 'group 'force)))
6925       ;; Clear the current group name.
6926       (unless quit-config
6927         (setq gnus-newsgroup-name nil)))))
6928
6929 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6930 (defun gnus-summary-exit-no-update (&optional no-questions)
6931   "Quit reading current newsgroup without updating read article info."
6932   (interactive)
6933   (let* ((group gnus-newsgroup-name)
6934          (gnus-group-is-exiting-p t)
6935          (gnus-group-is-exiting-without-update-p t)
6936          (quit-config (gnus-group-quit-config group)))
6937     (when (or no-questions
6938               gnus-expert-user
6939               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6940       (gnus-async-halt-prefetch)
6941       (run-hooks 'gnus-summary-prepare-exit-hook)
6942       (when (gnus-buffer-live-p gnus-article-buffer)
6943         (save-excursion
6944           (set-buffer gnus-article-buffer)
6945           (mm-destroy-parts gnus-article-mime-handles)
6946           ;; Set it to nil for safety reason.
6947           (setq gnus-article-mime-handle-alist nil)
6948           (setq gnus-article-mime-handles nil)))
6949       ;; If we have several article buffers, we kill them at exit.
6950       (unless gnus-single-article-buffer
6951         (gnus-kill-buffer gnus-article-buffer)
6952         (gnus-kill-buffer gnus-original-article-buffer)
6953         (setq gnus-article-current nil))
6954       (if (not gnus-kill-summary-on-exit)
6955           (gnus-deaden-summary)
6956         (gnus-close-group group)
6957         (gnus-summary-clear-local-variables)
6958         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6959           (gnus-summary-clear-local-variables))
6960         (gnus-kill-buffer gnus-summary-buffer))
6961       (unless gnus-single-article-buffer
6962         (setq gnus-article-current nil))
6963       (when gnus-use-trees
6964         (gnus-tree-close group))
6965       (gnus-async-prefetch-remove-group group)
6966       (when (get-buffer gnus-article-buffer)
6967         (bury-buffer gnus-article-buffer))
6968       ;; Return to the group buffer.
6969       (gnus-configure-windows 'group 'force)
6970       ;; Clear the current group name.
6971       (setq gnus-newsgroup-name nil)
6972       (unless (gnus-ephemeral-group-p group)
6973         (gnus-group-update-group group))
6974       (when (equal (gnus-group-group-name) group)
6975         (gnus-group-next-unread-group 1))
6976       (when quit-config
6977         (gnus-handle-ephemeral-exit quit-config)))))
6978
6979 (defun gnus-handle-ephemeral-exit (quit-config)
6980   "Handle movement when leaving an ephemeral group.
6981 The state which existed when entering the ephemeral is reset."
6982   (if (not (buffer-name (car quit-config)))
6983       (gnus-configure-windows 'group 'force)
6984     (set-buffer (car quit-config))
6985     (cond ((eq major-mode 'gnus-summary-mode)
6986            (gnus-set-global-variables))
6987           ((eq major-mode 'gnus-article-mode)
6988            (save-excursion
6989              ;; The `gnus-summary-buffer' variable may point
6990              ;; to the old summary buffer when using a single
6991              ;; article buffer.
6992              (unless (gnus-buffer-live-p gnus-summary-buffer)
6993                (set-buffer gnus-group-buffer))
6994              (set-buffer gnus-summary-buffer)
6995              (gnus-set-global-variables))))
6996     (if (or (eq (cdr quit-config) 'article)
6997             (eq (cdr quit-config) 'pick))
6998         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6999             (gnus-configure-windows 'pick 'force)
7000           (gnus-configure-windows (cdr quit-config) 'force))
7001       (gnus-configure-windows (cdr quit-config) 'force))
7002     (when (eq major-mode 'gnus-summary-mode)
7003       (gnus-summary-recenter)
7004       (gnus-summary-position-point))))
7005
7006 ;;; Dead summaries.
7007
7008 (defvar gnus-dead-summary-mode-map nil)
7009
7010 (unless gnus-dead-summary-mode-map
7011   (setq gnus-dead-summary-mode-map (make-keymap))
7012   (suppress-keymap gnus-dead-summary-mode-map)
7013   (substitute-key-definition
7014    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
7015   (dolist (key '("\C-d" "\r" "\177" [delete]))
7016     (define-key gnus-dead-summary-mode-map
7017       key 'gnus-summary-wake-up-the-dead))
7018   (dolist (key '("q" "Q"))
7019     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
7020
7021 (defvar gnus-dead-summary-mode nil
7022   "Minor mode for Gnus summary buffers.")
7023
7024 (defun gnus-dead-summary-mode (&optional arg)
7025   "Minor mode for Gnus summary buffers."
7026   (interactive "P")
7027   (when (eq major-mode 'gnus-summary-mode)
7028     (make-local-variable 'gnus-dead-summary-mode)
7029     (setq gnus-dead-summary-mode
7030           (if (null arg) (not gnus-dead-summary-mode)
7031             (> (prefix-numeric-value arg) 0)))
7032     (when gnus-dead-summary-mode
7033       (add-minor-mode
7034        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
7035
7036 (defun gnus-deaden-summary ()
7037   "Make the current summary buffer into a dead summary buffer."
7038   ;; Kill any previous dead summary buffer.
7039   (when (and gnus-dead-summary
7040              (buffer-name gnus-dead-summary))
7041     (with-current-buffer gnus-dead-summary
7042       (when gnus-dead-summary-mode
7043         (kill-buffer (current-buffer)))))
7044   ;; Make this the current dead summary.
7045   (setq gnus-dead-summary (current-buffer))
7046   (gnus-dead-summary-mode 1)
7047   (let ((name (buffer-name)))
7048     (when (string-match "Summary" name)
7049       (rename-buffer
7050        (concat (substring name 0 (match-beginning 0)) "Dead "
7051                (substring name (match-beginning 0)))
7052        t)
7053       (bury-buffer))))
7054
7055 (defun gnus-kill-or-deaden-summary (buffer)
7056   "Kill or deaden the summary BUFFER."
7057   (save-excursion
7058     (when (and (buffer-name buffer)
7059                (not gnus-single-article-buffer))
7060       (with-current-buffer buffer
7061         (gnus-kill-buffer gnus-article-buffer)
7062         (gnus-kill-buffer gnus-original-article-buffer)))
7063     (cond
7064      ;; Kill the buffer.
7065      (gnus-kill-summary-on-exit
7066       (when (and gnus-use-trees
7067                  (gnus-buffer-exists-p buffer))
7068         (save-excursion
7069           (set-buffer buffer)
7070           (gnus-tree-close gnus-newsgroup-name)))
7071       (gnus-kill-buffer buffer))
7072      ;; Deaden the buffer.
7073      ((gnus-buffer-exists-p buffer)
7074       (save-excursion
7075         (set-buffer buffer)
7076         (gnus-deaden-summary))))))
7077
7078 (defun gnus-summary-wake-up-the-dead (&rest args)
7079   "Wake up the dead summary buffer."
7080   (interactive)
7081   (gnus-dead-summary-mode -1)
7082   (let ((name (buffer-name)))
7083     (when (string-match "Dead " name)
7084       (rename-buffer
7085        (concat (substring name 0 (match-beginning 0))
7086                (substring name (match-end 0)))
7087        t)))
7088   (gnus-message 3 "This dead summary is now alive again"))
7089
7090 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7091 (defun gnus-summary-fetch-faq (&optional faq-dir)
7092   "Fetch the FAQ for the current group.
7093 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
7094 in."
7095   (interactive
7096    (list
7097     (when current-prefix-arg
7098       (completing-read
7099        "FAQ dir: " (and (listp gnus-group-faq-directory)
7100                         (mapcar 'list
7101                                 gnus-group-faq-directory))))))
7102   (let (gnus-faq-buffer)
7103     (when (setq gnus-faq-buffer
7104                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
7105       (gnus-configure-windows 'summary-faq))))
7106
7107 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7108 (defun gnus-summary-describe-group (&optional force)
7109   "Describe the current newsgroup."
7110   (interactive "P")
7111   (gnus-group-describe-group force gnus-newsgroup-name))
7112
7113 (defun gnus-summary-describe-briefly ()
7114   "Describe summary mode commands briefly."
7115   (interactive)
7116   (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")))
7117
7118 ;; Walking around group mode buffer from summary mode.
7119
7120 (defun gnus-summary-next-group (&optional no-article target-group backward)
7121   "Exit current newsgroup and then select next unread newsgroup.
7122 If prefix argument NO-ARTICLE is non-nil, no article is selected
7123 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7124 previous group instead."
7125   (interactive "P")
7126   ;; Stop pre-fetching.
7127   (gnus-async-halt-prefetch)
7128   (let ((current-group gnus-newsgroup-name)
7129         (current-buffer (current-buffer))
7130         entered)
7131     ;; First we semi-exit this group to update Xrefs and all variables.
7132     ;; We can't do a real exit, because the window conf must remain
7133     ;; the same in case the user is prompted for info, and we don't
7134     ;; want the window conf to change before that...
7135     (gnus-summary-exit t)
7136     (while (not entered)
7137       ;; Then we find what group we are supposed to enter.
7138       (set-buffer gnus-group-buffer)
7139       (gnus-group-jump-to-group current-group)
7140       (setq target-group
7141             (or target-group
7142                 (if (eq gnus-keep-same-level 'best)
7143                     (gnus-summary-best-group gnus-newsgroup-name)
7144                   (gnus-summary-search-group backward gnus-keep-same-level))))
7145       (if (not target-group)
7146           ;; There are no further groups, so we return to the group
7147           ;; buffer.
7148           (progn
7149             (gnus-message 5 "Returning to the group buffer")
7150             (setq entered t)
7151             (when (gnus-buffer-live-p current-buffer)
7152               (set-buffer current-buffer)
7153               (gnus-summary-exit))
7154             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7155         ;; We try to enter the target group.
7156         (gnus-group-jump-to-group target-group)
7157         (let ((unreads (gnus-group-group-unread)))
7158           (if (and (or (eq t unreads)
7159                        (and unreads (not (zerop unreads))))
7160                    (gnus-summary-read-group
7161                     target-group nil no-article
7162                     (and (buffer-name current-buffer) current-buffer)
7163                     nil backward))
7164               (setq entered t)
7165             (setq current-group target-group
7166                   target-group nil)))))))
7167
7168 (defun gnus-summary-prev-group (&optional no-article)
7169   "Exit current newsgroup and then select previous unread newsgroup.
7170 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7171   (interactive "P")
7172   (gnus-summary-next-group no-article nil t))
7173
7174 ;; Walking around summary lines.
7175
7176 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7177   "Go to the first subject satisfying any non-nil constraint.
7178 If UNREAD is non-nil, the article should be unread.
7179 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7180 If UNSEEN is non-nil, the article should be unseen.
7181 Returns the article selected or nil if there are no matching articles."
7182   (interactive "P")
7183   (cond
7184    ;; Empty summary.
7185    ((null gnus-newsgroup-data)
7186     (gnus-message 3 "No articles in the group")
7187     nil)
7188    ;; Pick the first article.
7189    ((not (or unread undownloaded unseen))
7190     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7191     (gnus-data-number (car gnus-newsgroup-data)))
7192    ;; Find the first unread article.
7193    (t
7194     (let ((data gnus-newsgroup-data))
7195       (while (and data
7196                   (let ((num (gnus-data-number (car data))))
7197                     (or (memq num gnus-newsgroup-unfetched)
7198                         (not (or (and unread
7199                                       (memq num gnus-newsgroup-unreads))
7200                                  (and undownloaded
7201                                       (memq num gnus-newsgroup-undownloaded))
7202                                  (and unseen
7203                                       (memq num gnus-newsgroup-unseen)))))))
7204         (setq data (cdr data)))
7205       (prog1
7206           (if data
7207               (progn
7208                 (goto-char (gnus-data-pos (car data)))
7209                 (gnus-data-number (car data)))
7210             (gnus-message 3 "No more%s articles"
7211                           (let* ((r (when unread " unread"))
7212                                  (d (when undownloaded " undownloaded"))
7213                                  (s (when unseen " unseen"))
7214                                  (l (delq nil (list r d s))))
7215                             (cond ((= 3 (length l))
7216                                    (concat r "," d ", or" s))
7217                                   ((= 2 (length l))
7218                                    (concat (car l) ", or" (cadr l)))
7219                                   ((= 1 (length l))
7220                                    (car l))
7221                                   (t
7222                                    ""))))
7223             nil
7224             )
7225         (gnus-summary-position-point))))))
7226
7227 (defun gnus-summary-next-subject (n &optional unread dont-display)
7228   "Go to next N'th summary line.
7229 If N is negative, go to the previous N'th subject line.
7230 If UNREAD is non-nil, only unread articles are selected.
7231 The difference between N and the actual number of steps taken is
7232 returned."
7233   (interactive "p")
7234   (let ((backward (< n 0))
7235         (n (abs n)))
7236     (while (and (> n 0)
7237                 (if backward
7238                     (gnus-summary-find-prev unread)
7239                   (gnus-summary-find-next unread)))
7240       (unless (zerop (setq n (1- n)))
7241         (gnus-summary-show-thread)))
7242     (when (/= 0 n)
7243       (gnus-message 7 "No more%s articles"
7244                     (if unread " unread" "")))
7245     (unless dont-display
7246       (gnus-summary-recenter)
7247       (gnus-summary-position-point))
7248     n))
7249
7250 (defun gnus-summary-next-unread-subject (n)
7251   "Go to next N'th unread summary line."
7252   (interactive "p")
7253   (gnus-summary-next-subject n t))
7254
7255 (defun gnus-summary-prev-subject (n &optional unread)
7256   "Go to previous N'th summary line.
7257 If optional argument UNREAD is non-nil, only unread article is selected."
7258   (interactive "p")
7259   (gnus-summary-next-subject (- n) unread))
7260
7261 (defun gnus-summary-prev-unread-subject (n)
7262   "Go to previous N'th unread summary line."
7263   (interactive "p")
7264   (gnus-summary-next-subject (- n) t))
7265
7266 (defun gnus-summary-goto-subjects (articles)
7267   "Insert the subject header for ARTICLES in the current buffer."
7268   (save-excursion
7269     (dolist (article articles)
7270       (gnus-summary-goto-subject article t)))
7271   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7272   (gnus-summary-position-point))
7273
7274 (defun gnus-summary-goto-subject (article &optional force silent)
7275   "Go the subject line of ARTICLE.
7276 If FORCE, also allow jumping to articles not currently shown."
7277   (interactive "nArticle number: ")
7278   (unless (numberp article)
7279     (error "Article %s is not a number" article))
7280   (let ((b (point))
7281         (data (gnus-data-find article)))
7282     ;; We read in the article if we have to.
7283     (and (not data)
7284          force
7285          (gnus-summary-insert-subject
7286           article
7287           (if (or (numberp force) (vectorp force)) force)
7288           t)
7289          (setq data (gnus-data-find article)))
7290     (goto-char b)
7291     (if (not data)
7292         (progn
7293           (unless silent
7294             (gnus-message 3 "Can't find article %d" article))
7295           nil)
7296       (let ((pt (gnus-data-pos data)))
7297         (goto-char pt)
7298         (gnus-summary-set-article-display-arrow pt))
7299       (gnus-summary-position-point)
7300       article)))
7301
7302 ;; Walking around summary lines with displaying articles.
7303
7304 (defun gnus-summary-expand-window (&optional arg)
7305   "Make the summary buffer take up the entire Emacs frame.
7306 Given a prefix, will force an `article' buffer configuration."
7307   (interactive "P")
7308   (if arg
7309       (gnus-configure-windows 'article 'force)
7310     (gnus-configure-windows 'summary 'force)))
7311
7312 (defun gnus-summary-display-article (article &optional all-header)
7313   "Display ARTICLE in article buffer."
7314   (when (gnus-buffer-live-p gnus-article-buffer)
7315     (with-current-buffer gnus-article-buffer
7316       (mm-enable-multibyte)))
7317   (gnus-set-global-variables)
7318   (when (gnus-buffer-live-p gnus-article-buffer)
7319     (with-current-buffer gnus-article-buffer
7320       (setq gnus-article-charset gnus-newsgroup-charset)
7321       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7322       (mm-enable-multibyte)))
7323   (if (null article)
7324       nil
7325     (prog1
7326         (if gnus-summary-display-article-function
7327             (funcall gnus-summary-display-article-function article all-header)
7328           (gnus-article-prepare article all-header))
7329       (gnus-run-hooks 'gnus-select-article-hook)
7330       (when (and gnus-current-article
7331                  (not (zerop gnus-current-article)))
7332         (gnus-summary-goto-subject gnus-current-article))
7333       (gnus-summary-recenter)
7334       (when (and gnus-use-trees gnus-show-threads)
7335         (gnus-possibly-generate-tree article)
7336         (gnus-highlight-selected-tree article))
7337       ;; Successfully display article.
7338       (gnus-article-set-window-start
7339        (cdr (assq article gnus-newsgroup-bookmarks))))))
7340
7341 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7342   "Select the current article.
7343 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7344 non-nil, the article will be re-fetched even if it already present in
7345 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7346 be displayed."
7347   ;; Make sure we are in the summary buffer to work around bbdb bug.
7348   (unless (eq major-mode 'gnus-summary-mode)
7349     (set-buffer gnus-summary-buffer))
7350   (let ((article (or article (gnus-summary-article-number)))
7351         (all-headers (not (not all-headers))) ;Must be t or nil.
7352         gnus-summary-display-article-function)
7353     (and (not pseudo)
7354          (gnus-summary-article-pseudo-p article)
7355          (error "This is a pseudo-article"))
7356     (save-excursion
7357       (set-buffer gnus-summary-buffer)
7358       (if (or (and gnus-single-article-buffer
7359                    (or (null gnus-current-article)
7360                        (null gnus-article-current)
7361                        (null (get-buffer gnus-article-buffer))
7362                        (not (eq article (cdr gnus-article-current)))
7363                        (not (equal (car gnus-article-current)
7364                                    gnus-newsgroup-name))))
7365               (and (not gnus-single-article-buffer)
7366                    (or (null gnus-current-article)
7367                        (not (eq gnus-current-article article))))
7368               force)
7369           ;; The requested article is different from the current article.
7370           (progn
7371             (gnus-summary-display-article article all-headers)
7372             (when (gnus-buffer-live-p gnus-article-buffer)
7373               (with-current-buffer gnus-article-buffer
7374                 (if (not gnus-article-decoded-p) ;; a local variable
7375                     (mm-disable-multibyte))))
7376             (gnus-article-set-window-start
7377              (cdr (assq article gnus-newsgroup-bookmarks)))
7378             article)
7379         'old))))
7380
7381 (defun gnus-summary-force-verify-and-decrypt ()
7382   "Display buttons for signed/encrypted parts and verify/decrypt them."
7383   (interactive)
7384   (let ((mm-verify-option 'known)
7385         (mm-decrypt-option 'known)
7386         (gnus-article-emulate-mime t)
7387         (gnus-buttonized-mime-types (append (list "multipart/signed"
7388                                                   "multipart/encrypted")
7389                                             gnus-buttonized-mime-types)))
7390     (gnus-summary-select-article nil 'force)))
7391
7392 (defun gnus-summary-set-current-mark (&optional current-mark)
7393   "Obsolete function."
7394   nil)
7395
7396 (defun gnus-summary-next-article (&optional unread subject backward push)
7397   "Select the next article.
7398 If UNREAD, only unread articles are selected.
7399 If SUBJECT, only articles with SUBJECT are selected.
7400 If BACKWARD, the previous article is selected instead of the next."
7401   (interactive "P")
7402   (cond
7403    ;; Is there such an article?
7404    ((and (gnus-summary-search-forward unread subject backward)
7405          (or (gnus-summary-display-article (gnus-summary-article-number))
7406              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7407     (gnus-summary-position-point))
7408    ;; If not, we try the first unread, if that is wanted.
7409    ((and subject
7410          gnus-auto-select-same
7411          (gnus-summary-first-unread-article))
7412     (gnus-summary-position-point)
7413     (gnus-message 6 "Wrapped"))
7414    ;; Try to get next/previous article not displayed in this group.
7415    ((and gnus-auto-extend-newsgroup
7416          (not unread) (not subject))
7417     (gnus-summary-goto-article
7418      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7419      nil (count-lines (point-min) (point))))
7420    ;; Go to next/previous group.
7421    (t
7422     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7423       (gnus-summary-jump-to-group gnus-newsgroup-name))
7424     (let ((cmd last-command-char)
7425           (point
7426            (with-current-buffer gnus-group-buffer
7427              (point)))
7428           (group
7429            (if (eq gnus-keep-same-level 'best)
7430                (gnus-summary-best-group gnus-newsgroup-name)
7431              (gnus-summary-search-group backward gnus-keep-same-level))))
7432       ;; For some reason, the group window gets selected.  We change
7433       ;; it back.
7434       (select-window (get-buffer-window (current-buffer)))
7435       ;; Select next unread newsgroup automagically.
7436       (cond
7437        ((or (not gnus-auto-select-next)
7438             (not cmd))
7439         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7440        ((or (eq gnus-auto-select-next 'quietly)
7441             (and (eq gnus-auto-select-next 'slightly-quietly)
7442                  push)
7443             (and (eq gnus-auto-select-next 'almost-quietly)
7444                  (gnus-summary-last-article-p)))
7445         ;; Select quietly.
7446         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7447             (gnus-summary-exit)
7448           (gnus-message 7 "No more%s articles (%s)..."
7449                         (if unread " unread" "")
7450                         (if group (concat "selecting " group)
7451                           "exiting"))
7452           (gnus-summary-next-group nil group backward)))
7453        (t
7454         (when (gnus-key-press-event-p last-input-event)
7455           (gnus-summary-walk-group-buffer
7456            gnus-newsgroup-name cmd unread backward point))))))))
7457
7458 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7459   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7460                       (?\C-p (gnus-group-prev-unread-group 1))))
7461         (cursor-in-echo-area t)
7462         keve key group ended prompt)
7463     (save-excursion
7464       (set-buffer gnus-group-buffer)
7465       (goto-char start)
7466       (setq group
7467             (if (eq gnus-keep-same-level 'best)
7468                 (gnus-summary-best-group gnus-newsgroup-name)
7469               (gnus-summary-search-group backward gnus-keep-same-level))))
7470     (while (not ended)
7471       (setq prompt
7472             (format
7473              "No more%s articles%s " (if unread " unread" "")
7474              (if (and group
7475                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7476                  (format " (Type %s for %s [%s])"
7477                          (single-key-description cmd)
7478                          (gnus-group-decoded-name group)
7479                          (gnus-group-unread group))
7480                (format " (Type %s to exit %s)"
7481                        (single-key-description cmd)
7482                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7483       ;; Confirm auto selection.
7484       (setq key (car (setq keve (gnus-read-event-char prompt)))
7485             ended t)
7486       (cond
7487        ((assq key keystrokes)
7488         (let ((obuf (current-buffer)))
7489           (switch-to-buffer gnus-group-buffer)
7490           (when group
7491             (gnus-group-jump-to-group group))
7492           (eval (cadr (assq key keystrokes)))
7493           (setq group (gnus-group-group-name))
7494           (switch-to-buffer obuf))
7495         (setq ended nil))
7496        ((equal key cmd)
7497         (if (or (not group)
7498                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7499             (gnus-summary-exit)
7500           (gnus-summary-next-group nil group backward)))
7501        (t
7502         (push (cdr keve) unread-command-events))))))
7503
7504 (defun gnus-summary-next-unread-article ()
7505   "Select unread article after current one."
7506   (interactive)
7507   (gnus-summary-next-article
7508    (or (not (eq gnus-summary-goto-unread 'never))
7509        (gnus-summary-last-article-p (gnus-summary-article-number)))
7510    (and gnus-auto-select-same
7511         (gnus-summary-article-subject))))
7512
7513 (defun gnus-summary-prev-article (&optional unread subject)
7514   "Select the article after the current one.
7515 If UNREAD is non-nil, only unread articles are selected."
7516   (interactive "P")
7517   (gnus-summary-next-article unread subject t))
7518
7519 (defun gnus-summary-prev-unread-article ()
7520   "Select unread article before current one."
7521   (interactive)
7522   (gnus-summary-prev-article
7523    (or (not (eq gnus-summary-goto-unread 'never))
7524        (gnus-summary-first-article-p (gnus-summary-article-number)))
7525    (and gnus-auto-select-same
7526         (gnus-summary-article-subject))))
7527
7528 (defun gnus-summary-next-page (&optional lines circular stop)
7529   "Show next page of the selected article.
7530 If at the end of the current article, select the next article.
7531 LINES says how many lines should be scrolled up.
7532
7533 If CIRCULAR is non-nil, go to the start of the article instead of
7534 selecting the next article when reaching the end of the current
7535 article.
7536
7537 If STOP is non-nil, just stop when reaching the end of the message.
7538
7539 Also see the variable `gnus-article-skip-boring'."
7540   (interactive "P")
7541   (setq gnus-summary-buffer (current-buffer))
7542   (gnus-set-global-variables)
7543   (let ((article (gnus-summary-article-number))
7544         (article-window (get-buffer-window gnus-article-buffer t))
7545         endp)
7546     ;; If the buffer is empty, we have no article.
7547     (unless article
7548       (error "No article to select"))
7549     (gnus-configure-windows 'article)
7550     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7551         (if (and (eq gnus-summary-goto-unread 'never)
7552                  (not (gnus-summary-last-article-p article)))
7553             (gnus-summary-next-article)
7554           (gnus-summary-next-unread-article))
7555       (if (or (null gnus-current-article)
7556               (null gnus-article-current)
7557               (/= article (cdr gnus-article-current))
7558               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7559           ;; Selected subject is different from current article's.
7560           (gnus-summary-display-article article)
7561         (when article-window
7562           (gnus-eval-in-buffer-window gnus-article-buffer
7563             (setq endp (or (gnus-article-next-page lines)
7564                            (gnus-article-only-boring-p))))
7565           (when endp
7566             (cond (stop
7567                    (gnus-message 3 "End of message"))
7568                   (circular
7569                    (gnus-summary-beginning-of-article))
7570                   (lines
7571                    (gnus-message 3 "End of message"))
7572                   ((null lines)
7573                    (if (and (eq gnus-summary-goto-unread 'never)
7574                             (not (gnus-summary-last-article-p article)))
7575                        (gnus-summary-next-article)
7576                      (gnus-summary-next-unread-article))))))))
7577     (gnus-summary-recenter)
7578     (gnus-summary-position-point)))
7579
7580 (defun gnus-summary-prev-page (&optional lines move)
7581   "Show previous page of selected article.
7582 Argument LINES specifies lines to be scrolled down.
7583 If MOVE, move to the previous unread article if point is at
7584 the beginning of the buffer."
7585   (interactive "P")
7586   (let ((article (gnus-summary-article-number))
7587         (article-window (get-buffer-window gnus-article-buffer t))
7588         endp)
7589     (gnus-configure-windows 'article)
7590     (if (or (null gnus-current-article)
7591             (null gnus-article-current)
7592             (/= article (cdr gnus-article-current))
7593             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7594         ;; Selected subject is different from current article's.
7595         (gnus-summary-display-article article)
7596       (gnus-summary-recenter)
7597       (when article-window
7598         (gnus-eval-in-buffer-window gnus-article-buffer
7599           (setq endp (gnus-article-prev-page lines)))
7600         (when (and move endp)
7601           (cond (lines
7602                  (gnus-message 3 "Beginning of message"))
7603                 ((null lines)
7604                  (if (and (eq gnus-summary-goto-unread 'never)
7605                           (not (gnus-summary-first-article-p article)))
7606                      (gnus-summary-prev-article)
7607                    (gnus-summary-prev-unread-article))))))))
7608   (gnus-summary-position-point))
7609
7610 (defun gnus-summary-prev-page-or-article (&optional lines)
7611   "Show previous page of selected article.
7612 Argument LINES specifies lines to be scrolled down.
7613 If at the beginning of the article, go to the next article."
7614   (interactive "P")
7615   (gnus-summary-prev-page lines t))
7616
7617 (defun gnus-summary-scroll-up (lines)
7618   "Scroll up (or down) one line current article.
7619 Argument LINES specifies lines to be scrolled up (or down if negative)."
7620   (interactive "p")
7621   (gnus-configure-windows 'article)
7622   (gnus-summary-show-thread)
7623   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7624     (gnus-eval-in-buffer-window gnus-article-buffer
7625       (cond ((> lines 0)
7626              (when (gnus-article-next-page lines)
7627                (gnus-message 3 "End of message")))
7628             ((< lines 0)
7629              (gnus-article-prev-page (- lines))))))
7630   (gnus-summary-recenter)
7631   (gnus-summary-position-point))
7632
7633 (defun gnus-summary-scroll-down (lines)
7634   "Scroll down (or up) one line current article.
7635 Argument LINES specifies lines to be scrolled down (or up if negative)."
7636   (interactive "p")
7637   (gnus-summary-scroll-up (- lines)))
7638
7639 (defun gnus-summary-next-same-subject ()
7640   "Select next article which has the same subject as current one."
7641   (interactive)
7642   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7643
7644 (defun gnus-summary-prev-same-subject ()
7645   "Select previous article which has the same subject as current one."
7646   (interactive)
7647   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7648
7649 (defun gnus-summary-next-unread-same-subject ()
7650   "Select next unread article which has the same subject as current one."
7651   (interactive)
7652   (gnus-summary-next-article t (gnus-summary-article-subject)))
7653
7654 (defun gnus-summary-prev-unread-same-subject ()
7655   "Select previous unread article which has the same subject as current one."
7656   (interactive)
7657   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7658
7659 (defun gnus-summary-first-unread-article ()
7660   "Select the first unread article.
7661 Return nil if there are no unread articles."
7662   (interactive)
7663   (prog1
7664       (when (gnus-summary-first-subject t)
7665         (gnus-summary-show-thread)
7666         (gnus-summary-first-subject t)
7667         (gnus-summary-display-article (gnus-summary-article-number)))
7668     (gnus-summary-position-point)))
7669
7670 (defun gnus-summary-first-unread-subject ()
7671   "Place the point on the subject line of the first unread article.
7672 Return nil if there are no unread articles."
7673   (interactive)
7674   (prog1
7675       (when (gnus-summary-first-subject t)
7676         (gnus-summary-show-thread)
7677         (gnus-summary-first-subject t))
7678     (gnus-summary-position-point)))
7679
7680 (defun gnus-summary-first-unseen-subject ()
7681   "Place the point on the subject line of the first unseen article.
7682 Return nil if there are no unseen articles."
7683   (interactive)
7684   (prog1
7685       (when (gnus-summary-first-subject nil nil t)
7686         (gnus-summary-show-thread)
7687         (gnus-summary-first-subject nil nil t))
7688     (gnus-summary-position-point)))
7689
7690 (defun gnus-summary-first-unseen-or-unread-subject ()
7691   "Place the point on the subject line of the first unseen article or,
7692 if all article have been seen, on the subject line of the first unread
7693 article."
7694   (interactive)
7695   (prog1
7696       (unless (when (gnus-summary-first-subject nil nil t)
7697                 (gnus-summary-show-thread)
7698                 (gnus-summary-first-subject nil nil t))
7699         (when (gnus-summary-first-subject t)
7700           (gnus-summary-show-thread)
7701           (gnus-summary-first-subject t)))
7702     (gnus-summary-position-point)))
7703
7704 (defun gnus-summary-first-article ()
7705   "Select the first article.
7706 Return nil if there are no articles."
7707   (interactive)
7708   (prog1
7709       (when (gnus-summary-first-subject)
7710         (gnus-summary-show-thread)
7711         (gnus-summary-first-subject)
7712         (gnus-summary-display-article (gnus-summary-article-number)))
7713     (gnus-summary-position-point)))
7714
7715 (defun gnus-summary-best-unread-article (&optional arg)
7716   "Select the unread article with the highest score.
7717 If given a prefix argument, select the next unread article that has a
7718 score higher than the default score."
7719   (interactive "P")
7720   (let ((article (if arg
7721                      (gnus-summary-better-unread-subject)
7722                    (gnus-summary-best-unread-subject))))
7723     (if article
7724         (gnus-summary-goto-article article)
7725       (error "No unread articles"))))
7726
7727 (defun gnus-summary-best-unread-subject ()
7728   "Select the unread subject with the highest score."
7729   (interactive)
7730   (let ((best -1000000)
7731         (data gnus-newsgroup-data)
7732         article score)
7733     (while data
7734       (and (gnus-data-unread-p (car data))
7735            (> (setq score
7736                     (gnus-summary-article-score (gnus-data-number (car data))))
7737               best)
7738            (setq best score
7739                  article (gnus-data-number (car data))))
7740       (setq data (cdr data)))
7741     (when article
7742       (gnus-summary-goto-subject article))
7743     (gnus-summary-position-point)
7744     article))
7745
7746 (defun gnus-summary-better-unread-subject ()
7747   "Select the first unread subject that has a score over the default score."
7748   (interactive)
7749   (let ((data gnus-newsgroup-data)
7750         article score)
7751     (while (and (setq article (gnus-data-number (car data)))
7752                 (or (gnus-data-read-p (car data))
7753                     (not (> (gnus-summary-article-score article)
7754                             gnus-summary-default-score))))
7755       (setq data (cdr data)))
7756     (when article
7757       (gnus-summary-goto-subject article))
7758     (gnus-summary-position-point)
7759     article))
7760
7761 (defun gnus-summary-last-subject ()
7762   "Go to the last displayed subject line in the group."
7763   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7764     (when article
7765       (gnus-summary-goto-subject article))))
7766
7767 (defun gnus-summary-goto-article (article &optional all-headers force)
7768   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7769 If ALL-HEADERS is non-nil, no header lines are hidden.
7770 If FORCE, go to the article even if it isn't displayed.  If FORCE
7771 is a number, it is the line the article is to be displayed on."
7772   (interactive
7773    (list
7774     (completing-read
7775      "Article number or Message-ID: "
7776      (mapcar (lambda (number) (list (int-to-string number)))
7777              gnus-newsgroup-limit))
7778     current-prefix-arg
7779     t))
7780   (prog1
7781       (if (and (stringp article)
7782                (string-match "@\\|%40" article))
7783           (gnus-summary-refer-article article)
7784         (when (stringp article)
7785           (setq article (string-to-number article)))
7786         (if (gnus-summary-goto-subject article force)
7787             (gnus-summary-display-article article all-headers)
7788           (gnus-message 4 "Couldn't go to article %s" article) nil))
7789     (gnus-summary-position-point)))
7790
7791 (defun gnus-summary-goto-last-article ()
7792   "Go to the previously read article."
7793   (interactive)
7794   (prog1
7795       (when gnus-last-article
7796         (gnus-summary-goto-article gnus-last-article nil t))
7797     (gnus-summary-position-point)))
7798
7799 (defun gnus-summary-pop-article (number)
7800   "Pop one article off the history and go to the previous.
7801 NUMBER articles will be popped off."
7802   (interactive "p")
7803   (let (to)
7804     (setq gnus-newsgroup-history
7805           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7806     (if to
7807         (gnus-summary-goto-article (car to) nil t)
7808       (error "Article history empty")))
7809   (gnus-summary-position-point))
7810
7811 ;; Summary commands and functions for limiting the summary buffer.
7812
7813 (defun gnus-summary-limit-to-articles (n)
7814   "Limit the summary buffer to the next N articles.
7815 If not given a prefix, use the process marked articles instead."
7816   (interactive "P")
7817   (prog1
7818       (let ((articles (gnus-summary-work-articles n)))
7819         (setq gnus-newsgroup-processable nil)
7820         (gnus-summary-limit articles))
7821     (gnus-summary-position-point)))
7822
7823 (defun gnus-summary-pop-limit (&optional total)
7824   "Restore the previous limit.
7825 If given a prefix, remove all limits."
7826   (interactive "P")
7827   (when total
7828     (setq gnus-newsgroup-limits
7829           (list (mapcar (lambda (h) (mail-header-number h))
7830                         gnus-newsgroup-headers))))
7831   (unless gnus-newsgroup-limits
7832     (error "No limit to pop"))
7833   (prog1
7834       (gnus-summary-limit nil 'pop)
7835     (gnus-summary-position-point)))
7836
7837 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7838   "Limit the summary buffer to articles that have subjects that match a regexp.
7839 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7840   (interactive
7841    (list (read-string (if current-prefix-arg
7842                           "Exclude subject (regexp): "
7843                         "Limit to subject (regexp): "))
7844          nil current-prefix-arg))
7845   (unless header
7846     (setq header "subject"))
7847   (when (not (equal "" subject))
7848     (prog1
7849         (let ((articles (gnus-summary-find-matching
7850                          (or header "subject") subject 'all nil nil
7851                          not-matching)))
7852           (unless articles
7853             (error "Found no matches for \"%s\"" subject))
7854           (gnus-summary-limit articles))
7855       (gnus-summary-position-point))))
7856
7857 (defun gnus-summary-limit-to-author (from &optional not-matching)
7858   "Limit the summary buffer to articles that have authors that match a regexp.
7859 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7860   (interactive
7861    (list (read-string (if current-prefix-arg
7862                           "Exclude author (regexp): "
7863                         "Limit to author (regexp): "))
7864          current-prefix-arg))
7865   (gnus-summary-limit-to-subject from "from" not-matching))
7866
7867 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
7868   "Limit the summary buffer to articles with the given RECIPIENT.
7869
7870 If NOT-MATCHING, exclude RECIPIENT.
7871
7872 To and Cc headers are checked.  You need to include them in
7873 `nnmail-extra-headers'."
7874   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
7875   (interactive
7876    (list (read-string (format "%s recipient (regexp): "
7877                               (if current-prefix-arg "Exclude" "Limit to")))
7878          current-prefix-arg))
7879   (when (not (equal "" recipient))
7880     (prog1 (let* ((to
7881                    (if (memq 'To nnmail-extra-headers)
7882                        (gnus-summary-find-matching
7883                         (cons 'extra 'To) recipient 'all nil nil
7884                         not-matching)
7885                      (gnus-message
7886                       1 "`To' isn't present in `nnmail-extra-headers'")
7887                      (sit-for 1)
7888                      nil))
7889                   (cc
7890                    (if (memq 'Cc nnmail-extra-headers)
7891                        (gnus-summary-find-matching
7892                         (cons 'extra 'Cc) recipient 'all nil nil
7893                         not-matching)
7894                      (gnus-message
7895                       1 "`Cc' isn't present in `nnmail-extra-headers'")
7896                      (sit-for 1)
7897                      nil))
7898                   (articles
7899                    (if not-matching
7900                        ;; We need the numbers that are in both lists:
7901                        (mapcar (lambda (a)
7902                                  (and (memq a to) a))
7903                                cc)
7904                      (nconc to cc))))
7905              (unless articles
7906                (error "Found no matches for \"%s\"" recipient))
7907              (gnus-summary-limit articles))
7908       (gnus-summary-position-point))))
7909
7910 (defun gnus-summary-limit-to-age (age &optional younger-p)
7911   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7912 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7913 articles that are younger than AGE days."
7914   (interactive
7915    (let ((younger current-prefix-arg)
7916          (days-got nil)
7917          days)
7918      (while (not days-got)
7919        (setq days (if younger
7920                       (read-string "Limit to articles younger than (in days, older when negative): ")
7921                     (read-string
7922                      "Limit to articles older than (in days, younger when negative): ")))
7923        (when (> (length days) 0)
7924          (setq days (read days)))
7925        (if (numberp days)
7926            (progn
7927              (setq days-got t)
7928              (when (< days 0)
7929                (setq younger (not younger))
7930                (setq days (* days -1))))
7931          (message "Please enter a number.")
7932          (sleep-for 1)))
7933      (list days younger)))
7934   (prog1
7935       (let ((data gnus-newsgroup-data)
7936             (cutoff (days-to-time age))
7937             articles d date is-younger)
7938         (while (setq d (pop data))
7939           (when (and (vectorp (gnus-data-header d))
7940                      (setq date (mail-header-date (gnus-data-header d))))
7941             (setq is-younger (time-less-p
7942                               (time-since (condition-case ()
7943                                               (date-to-time date)
7944                                             (error '(0 0))))
7945                               cutoff))
7946             (when (if younger-p
7947                       is-younger
7948                     (not is-younger))
7949               (push (gnus-data-number d) articles))))
7950         (gnus-summary-limit (nreverse articles)))
7951     (gnus-summary-position-point)))
7952
7953 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7954   "Limit the summary buffer to articles that match an 'extra' header."
7955   (interactive
7956    (let ((header
7957           (intern
7958            (gnus-completing-read-with-default
7959             (symbol-name (car gnus-extra-headers))
7960             (if current-prefix-arg
7961                 "Exclude extra header"
7962               "Limit extra header")
7963             (mapcar (lambda (x)
7964                       (cons (symbol-name x) x))
7965                     gnus-extra-headers)
7966             nil
7967             t))))
7968      (list header
7969            (read-string (format "%s header %s (regexp): "
7970                                 (if current-prefix-arg "Exclude" "Limit to")
7971                                 header))
7972            current-prefix-arg)))
7973   (when (not (equal "" regexp))
7974     (prog1
7975         (let ((articles (gnus-summary-find-matching
7976                          (cons 'extra header) regexp 'all nil nil
7977                          not-matching)))
7978           (unless articles
7979             (error "Found no matches for \"%s\"" regexp))
7980           (gnus-summary-limit articles))
7981       (gnus-summary-position-point))))
7982
7983 (defun gnus-summary-limit-to-display-predicate ()
7984   "Limit the summary buffer to the predicated in the `display' group parameter."
7985   (interactive)
7986   (unless gnus-newsgroup-display
7987     (error "There is no `display' group parameter"))
7988   (let (articles)
7989     (dolist (number gnus-newsgroup-articles)
7990       (when (funcall gnus-newsgroup-display)
7991         (push number articles)))
7992     (gnus-summary-limit articles))
7993   (gnus-summary-position-point))
7994
7995 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7996 (make-obsolete
7997  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7998
7999 (defun gnus-summary-limit-to-unread (&optional all)
8000   "Limit the summary buffer to articles that are not marked as read.
8001 If ALL is non-nil, limit strictly to unread articles."
8002   (interactive "P")
8003   (if all
8004       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8005     (gnus-summary-limit-to-marks
8006      ;; Concat all the marks that say that an article is read and have
8007      ;; those removed.
8008      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8009            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8010            gnus-low-score-mark gnus-expirable-mark
8011            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8012            gnus-duplicate-mark gnus-souped-mark)
8013      'reverse)))
8014
8015 (defun gnus-summary-limit-to-bodies (match &optional reverse)
8016   "Limit the summary buffer to articles that have bodies that match MATCH.
8017 If REVERSE (the prefix), limit to articles that don't match."
8018   (interactive "sMatch body (regexp): \nP")
8019   (let ((articles nil)
8020         (gnus-select-article-hook nil)  ;Disable hook.
8021         (gnus-article-prepare-hook nil)
8022         (gnus-use-article-prefetch nil)
8023         (gnus-keep-backlog nil)
8024         (gnus-break-pages nil)
8025         (gnus-summary-display-arrow nil)
8026         (gnus-updated-mode-lines nil)
8027         (gnus-auto-center-summary nil)
8028         (gnus-display-mime-function nil))
8029     (dolist (data gnus-newsgroup-data)
8030       (let (gnus-mark-article-hook)
8031         (gnus-summary-select-article t t nil (gnus-data-number data)))
8032       (save-excursion
8033         (set-buffer gnus-article-buffer)
8034         (article-goto-body)
8035         (let* ((case-fold-search t)
8036                (found (re-search-forward match nil t)))
8037           (when (or (and found
8038                          (not reverse))
8039                     (and (not found)
8040                          reverse))
8041             (push (gnus-data-number data) articles)))))
8042     (if (not articles)
8043         (message "No messages matched")
8044       (gnus-summary-limit articles)))
8045   (gnus-summary-position-point))
8046
8047 (defun gnus-summary-limit-to-replied (&optional unreplied)
8048   "Limit the summary buffer to replied articles.
8049 If UNREPLIED (the prefix), limit to unreplied articles."
8050   (interactive "P")
8051   (if unreplied
8052       (gnus-summary-limit
8053        (gnus-set-difference gnus-newsgroup-articles
8054         gnus-newsgroup-replied))
8055     (gnus-summary-limit gnus-newsgroup-replied))
8056   (gnus-summary-position-point))
8057
8058 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8059 (make-obsolete 'gnus-summary-delete-marked-with
8060                'gnus-summary-limit-exclude-marks)
8061
8062 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8063   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8064 If REVERSE, limit the summary buffer to articles that are marked
8065 with MARKS.  MARKS can either be a string of marks or a list of marks.
8066 Returns how many articles were removed."
8067   (interactive "sMarks: ")
8068   (gnus-summary-limit-to-marks marks t))
8069
8070 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8071   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8072 If REVERSE (the prefix), limit the summary buffer to articles that are
8073 not marked with MARKS.  MARKS can either be a string of marks or a
8074 list of marks.
8075 Returns how many articles were removed."
8076   (interactive "sMarks: \nP")
8077   (prog1
8078       (let ((data gnus-newsgroup-data)
8079             (marks (if (listp marks) marks
8080                      (append marks nil))) ; Transform to list.
8081             articles)
8082         (while data
8083           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8084                   (memq (gnus-data-mark (car data)) marks))
8085             (push (gnus-data-number (car data)) articles))
8086           (setq data (cdr data)))
8087         (gnus-summary-limit articles))
8088     (gnus-summary-position-point)))
8089
8090 (defun gnus-summary-limit-to-score (score)
8091   "Limit to articles with score at or above SCORE."
8092   (interactive "NLimit to articles with score of at least: ")
8093   (let ((data gnus-newsgroup-data)
8094         articles)
8095     (while data
8096       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8097                 score)
8098         (push (gnus-data-number (car data)) articles))
8099       (setq data (cdr data)))
8100     (prog1
8101         (gnus-summary-limit articles)
8102       (gnus-summary-position-point))))
8103
8104 (defun gnus-summary-limit-to-unseen ()
8105   "Limit to unseen articles."
8106   (interactive)
8107   (prog1
8108       (gnus-summary-limit gnus-newsgroup-unseen)
8109     (gnus-summary-position-point)))
8110
8111 (defun gnus-summary-limit-include-thread (id)
8112   "Display all the hidden articles that is in the thread with ID in it.
8113 When called interactively, ID is the Message-ID of the current
8114 article."
8115   (interactive (list (mail-header-id (gnus-summary-article-header))))
8116   (let ((articles (gnus-articles-in-thread
8117                    (gnus-id-to-thread (gnus-root-id id)))))
8118     (prog1
8119         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8120       (gnus-summary-limit-include-matching-articles
8121        "subject"
8122        (regexp-quote (gnus-simplify-subject-re
8123                       (mail-header-subject (gnus-id-to-header id)))))
8124       (gnus-summary-position-point))))
8125
8126 (defun gnus-summary-limit-include-matching-articles (header regexp)
8127   "Display all the hidden articles that have HEADERs that match REGEXP."
8128   (interactive (list (read-string "Match on header: ")
8129                      (read-string "Regexp: ")))
8130   (let ((articles (gnus-find-matching-articles header regexp)))
8131     (prog1
8132         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8133       (gnus-summary-position-point))))
8134
8135 (defun gnus-summary-insert-dormant-articles ()
8136   "Insert all the dormant articles for this group into the current buffer."
8137   (interactive)
8138   (let ((gnus-verbose (max 6 gnus-verbose)))
8139     (if (not gnus-newsgroup-dormant)
8140         (gnus-message 3 "No cached articles for this group")
8141       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8142
8143 (defun gnus-summary-limit-include-dormant ()
8144   "Display all the hidden articles that are marked as dormant.
8145 Note that this command only works on a subset of the articles currently
8146 fetched for this group."
8147   (interactive)
8148   (unless gnus-newsgroup-dormant
8149     (error "There are no dormant articles in this group"))
8150   (prog1
8151       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8152     (gnus-summary-position-point)))
8153
8154 (defun gnus-summary-limit-exclude-dormant ()
8155   "Hide all dormant articles."
8156   (interactive)
8157   (prog1
8158       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8159     (gnus-summary-position-point)))
8160
8161 (defun gnus-summary-limit-exclude-childless-dormant ()
8162   "Hide all dormant articles that have no children."
8163   (interactive)
8164   (let ((data (gnus-data-list t))
8165         articles d children)
8166     ;; Find all articles that are either not dormant or have
8167     ;; children.
8168     (while (setq d (pop data))
8169       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8170                 (and (setq children
8171                            (gnus-article-children (gnus-data-number d)))
8172                      (let (found)
8173                        (while children
8174                          (when (memq (car children) articles)
8175                            (setq children nil
8176                                  found t))
8177                          (pop children))
8178                        found)))
8179         (push (gnus-data-number d) articles)))
8180     ;; Do the limiting.
8181     (prog1
8182         (gnus-summary-limit articles)
8183       (gnus-summary-position-point))))
8184
8185 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8186   "Mark all unread excluded articles as read.
8187 If ALL, mark even excluded ticked and dormants as read."
8188   (interactive "P")
8189   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8190   (let ((articles (gnus-sorted-ndifference
8191                    (sort
8192                     (mapcar (lambda (h) (mail-header-number h))
8193                             gnus-newsgroup-headers)
8194                     '<)
8195                    gnus-newsgroup-limit))
8196         article)
8197     (setq gnus-newsgroup-unreads
8198           (gnus-sorted-intersection gnus-newsgroup-unreads
8199                                     gnus-newsgroup-limit))
8200     (if all
8201         (setq gnus-newsgroup-dormant nil
8202               gnus-newsgroup-marked nil
8203               gnus-newsgroup-reads
8204               (nconc
8205                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8206                gnus-newsgroup-reads))
8207       (while (setq article (pop articles))
8208         (unless (or (memq article gnus-newsgroup-dormant)
8209                     (memq article gnus-newsgroup-marked))
8210           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8211
8212 (defun gnus-summary-limit (articles &optional pop)
8213   (if pop
8214       ;; We pop the previous limit off the stack and use that.
8215       (setq articles (car gnus-newsgroup-limits)
8216             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8217     ;; We use the new limit, so we push the old limit on the stack.
8218     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8219   ;; Set the limit.
8220   (setq gnus-newsgroup-limit articles)
8221   (let ((total (length gnus-newsgroup-data))
8222         (data (gnus-data-find-list (gnus-summary-article-number)))
8223         (gnus-summary-mark-below nil)   ; Inhibit this.
8224         found)
8225     ;; This will do all the work of generating the new summary buffer
8226     ;; according to the new limit.
8227     (gnus-summary-prepare)
8228     ;; Hide any threads, possibly.
8229     (gnus-summary-maybe-hide-threads)
8230     ;; Try to return to the article you were at, or one in the
8231     ;; neighborhood.
8232     (when data
8233       ;; We try to find some article after the current one.
8234       (while data
8235         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8236           (setq data nil
8237                 found t))
8238         (setq data (cdr data))))
8239     (unless found
8240       ;; If there is no data, that means that we were after the last
8241       ;; article.  The same goes when we can't find any articles
8242       ;; after the current one.
8243       (goto-char (point-max))
8244       (gnus-summary-find-prev))
8245     (gnus-set-mode-line 'summary)
8246     ;; We return how many articles were removed from the summary
8247     ;; buffer as a result of the new limit.
8248     (- total (length gnus-newsgroup-data))))
8249
8250 (defsubst gnus-invisible-cut-children (threads)
8251   (let ((num 0))
8252     (while threads
8253       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8254         (incf num))
8255       (pop threads))
8256     (< num 2)))
8257
8258 (defsubst gnus-cut-thread (thread)
8259   "Go forwards in the thread until we find an article that we want to display."
8260   (when (or (eq gnus-fetch-old-headers 'some)
8261             (eq gnus-fetch-old-headers 'invisible)
8262             (numberp gnus-fetch-old-headers)
8263             (eq gnus-build-sparse-threads 'some)
8264             (eq gnus-build-sparse-threads 'more))
8265     ;; Deal with old-fetched headers and sparse threads.
8266     (while (and
8267             thread
8268             (or
8269              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8270              (gnus-summary-article-ancient-p
8271               (mail-header-number (car thread))))
8272             (if (or (<= (length (cdr thread)) 1)
8273                     (eq gnus-fetch-old-headers 'invisible))
8274                 (setq gnus-newsgroup-limit
8275                       (delq (mail-header-number (car thread))
8276                             gnus-newsgroup-limit)
8277                       thread (cadr thread))
8278               (when (gnus-invisible-cut-children (cdr thread))
8279                 (let ((th (cdr thread)))
8280                   (while th
8281                     (if (memq (mail-header-number (caar th))
8282                               gnus-newsgroup-limit)
8283                         (setq thread (car th)
8284                               th nil)
8285                       (setq th (cdr th))))))))))
8286   thread)
8287
8288 (defun gnus-cut-threads (threads)
8289   "Cut off all uninteresting articles from the beginning of THREADS."
8290   (when (or (eq gnus-fetch-old-headers 'some)
8291             (eq gnus-fetch-old-headers 'invisible)
8292             (numberp gnus-fetch-old-headers)
8293             (eq gnus-build-sparse-threads 'some)
8294             (eq gnus-build-sparse-threads 'more))
8295     (let ((th threads))
8296       (while th
8297         (setcar th (gnus-cut-thread (car th)))
8298         (setq th (cdr th)))))
8299   ;; Remove nixed out threads.
8300   (delq nil threads))
8301
8302 (defun gnus-summary-initial-limit (&optional show-if-empty)
8303   "Figure out what the initial limit is supposed to be on group entry.
8304 This entails weeding out unwanted dormants, low-scored articles,
8305 fetch-old-headers verbiage, and so on."
8306   ;; Most groups have nothing to remove.
8307   (if (or gnus-inhibit-limiting
8308           (and (null gnus-newsgroup-dormant)
8309                (eq gnus-newsgroup-display 'gnus-not-ignore)
8310                (not (eq gnus-fetch-old-headers 'some))
8311                (not (numberp gnus-fetch-old-headers))
8312                (not (eq gnus-fetch-old-headers 'invisible))
8313                (null gnus-summary-expunge-below)
8314                (not (eq gnus-build-sparse-threads 'some))
8315                (not (eq gnus-build-sparse-threads 'more))
8316                (null gnus-thread-expunge-below)
8317                (not gnus-use-nocem)))
8318       ()                                ; Do nothing.
8319     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8320     (setq gnus-newsgroup-limit nil)
8321     (mapatoms
8322      (lambda (node)
8323        (unless (car (symbol-value node))
8324          ;; These threads have no parents -- they are roots.
8325          (let ((nodes (cdr (symbol-value node)))
8326                thread)
8327            (while nodes
8328              (if (and gnus-thread-expunge-below
8329                       (< (gnus-thread-total-score (car nodes))
8330                          gnus-thread-expunge-below))
8331                  (gnus-expunge-thread (pop nodes))
8332                (setq thread (pop nodes))
8333                (gnus-summary-limit-children thread))))))
8334      gnus-newsgroup-dependencies)
8335     ;; If this limitation resulted in an empty group, we might
8336     ;; pop the previous limit and use it instead.
8337     (when (and (not gnus-newsgroup-limit)
8338                show-if-empty)
8339       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8340     gnus-newsgroup-limit))
8341
8342 (defun gnus-summary-limit-children (thread)
8343   "Return 1 if this subthread is visible and 0 if it is not."
8344   ;; First we get the number of visible children to this thread.  This
8345   ;; is done by recursing down the thread using this function, so this
8346   ;; will really go down to a leaf article first, before slowly
8347   ;; working its way up towards the root.
8348   (when thread
8349     (let* ((max-lisp-eval-depth 5000)
8350            (children
8351            (if (cdr thread)
8352                (apply '+ (mapcar 'gnus-summary-limit-children
8353                                  (cdr thread)))
8354              0))
8355           (number (mail-header-number (car thread)))
8356           score)
8357       (if (and
8358            (not (memq number gnus-newsgroup-marked))
8359            (or
8360             ;; If this article is dormant and has absolutely no visible
8361             ;; children, then this article isn't visible.
8362             (and (memq number gnus-newsgroup-dormant)
8363                  (zerop children))
8364             ;; If this is "fetch-old-headered" and there is no
8365             ;; visible children, then we don't want this article.
8366             (and (or (eq gnus-fetch-old-headers 'some)
8367                      (numberp gnus-fetch-old-headers))
8368                  (gnus-summary-article-ancient-p number)
8369                  (zerop children))
8370             ;; If this is "fetch-old-headered" and `invisible', then
8371             ;; we don't want this article.
8372             (and (eq gnus-fetch-old-headers 'invisible)
8373                  (gnus-summary-article-ancient-p number))
8374             ;; If this is a sparsely inserted article with no children,
8375             ;; we don't want it.
8376             (and (eq gnus-build-sparse-threads 'some)
8377                  (gnus-summary-article-sparse-p number)
8378                  (zerop children))
8379             ;; If we use expunging, and this article is really
8380             ;; low-scored, then we don't want this article.
8381             (when (and gnus-summary-expunge-below
8382                        (< (setq score
8383                                 (or (cdr (assq number gnus-newsgroup-scored))
8384                                     gnus-summary-default-score))
8385                           gnus-summary-expunge-below))
8386               ;; We increase the expunge-tally here, but that has
8387               ;; nothing to do with the limits, really.
8388               (incf gnus-newsgroup-expunged-tally)
8389               ;; We also mark as read here, if that's wanted.
8390               (when (and gnus-summary-mark-below
8391                          (< score gnus-summary-mark-below))
8392                 (setq gnus-newsgroup-unreads
8393                       (delq number gnus-newsgroup-unreads))
8394                 (if gnus-newsgroup-auto-expire
8395                     (push number gnus-newsgroup-expirable)
8396                   (push (cons number gnus-low-score-mark)
8397                         gnus-newsgroup-reads)))
8398               t)
8399             ;; Do the `display' group parameter.
8400             (and gnus-newsgroup-display
8401                  (not (funcall gnus-newsgroup-display)))
8402             ;; Check NoCeM things.
8403             (when (and gnus-use-nocem
8404                        (gnus-nocem-unwanted-article-p
8405                         (mail-header-id (car thread))))
8406               (setq gnus-newsgroup-unreads
8407                     (delq number gnus-newsgroup-unreads))
8408               t)))
8409           ;; Nope, invisible article.
8410           0
8411         ;; Ok, this article is to be visible, so we add it to the limit
8412         ;; and return 1.
8413         (push number gnus-newsgroup-limit)
8414         1))))
8415
8416 (defun gnus-expunge-thread (thread)
8417   "Mark all articles in THREAD as read."
8418   (let* ((number (mail-header-number (car thread))))
8419     (incf gnus-newsgroup-expunged-tally)
8420     ;; We also mark as read here, if that's wanted.
8421     (setq gnus-newsgroup-unreads
8422           (delq number gnus-newsgroup-unreads))
8423     (if gnus-newsgroup-auto-expire
8424         (push number gnus-newsgroup-expirable)
8425       (push (cons number gnus-low-score-mark)
8426             gnus-newsgroup-reads)))
8427   ;; Go recursively through all subthreads.
8428   (mapcar 'gnus-expunge-thread (cdr thread)))
8429
8430 ;; Summary article oriented commands
8431
8432 (defun gnus-summary-refer-parent-article (n)
8433   "Refer parent article N times.
8434 If N is negative, go to ancestor -N instead.
8435 The difference between N and the number of articles fetched is returned."
8436   (interactive "p")
8437   (let ((skip 1)
8438         error header ref)
8439     (when (not (natnump n))
8440       (setq skip (abs n)
8441             n 1))
8442     (while (and (> n 0)
8443                 (not error))
8444       (setq header (gnus-summary-article-header))
8445       (if (and (eq (mail-header-number header)
8446                    (cdr gnus-article-current))
8447                (equal gnus-newsgroup-name
8448                       (car gnus-article-current)))
8449           ;; If we try to find the parent of the currently
8450           ;; displayed article, then we take a look at the actual
8451           ;; References header, since this is slightly more
8452           ;; reliable than the References field we got from the
8453           ;; server.
8454           (save-excursion
8455             (set-buffer gnus-original-article-buffer)
8456             (nnheader-narrow-to-headers)
8457             (unless (setq ref (message-fetch-field "references"))
8458               (when (setq ref (message-fetch-field "in-reply-to"))
8459                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8460             (widen))
8461         (setq ref
8462               ;; It's not the current article, so we take a bet on
8463               ;; the value we got from the server.
8464               (mail-header-references header)))
8465       (if (and ref
8466                (not (equal ref "")))
8467           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8468             (gnus-message 1 "Couldn't find parent"))
8469         (gnus-message 1 "No references in article %d"
8470                       (gnus-summary-article-number))
8471         (setq error t))
8472       (decf n))
8473     (gnus-summary-position-point)
8474     n))
8475
8476 (defun gnus-summary-refer-references ()
8477   "Fetch all articles mentioned in the References header.
8478 Return the number of articles fetched."
8479   (interactive)
8480   (let ((ref (mail-header-references (gnus-summary-article-header)))
8481         (current (gnus-summary-article-number))
8482         (n 0))
8483     (if (or (not ref)
8484             (equal ref ""))
8485         (error "No References in the current article")
8486       ;; For each Message-ID in the References header...
8487       (while (string-match "<[^>]*>" ref)
8488         (incf n)
8489         ;; ... fetch that article.
8490         (gnus-summary-refer-article
8491          (prog1 (match-string 0 ref)
8492            (setq ref (substring ref (match-end 0))))))
8493       (gnus-summary-goto-subject current)
8494       (gnus-summary-position-point)
8495       n)))
8496
8497 (defun gnus-summary-refer-thread (&optional limit)
8498   "Fetch all articles in the current thread.
8499 If LIMIT (the numerical prefix), fetch that many old headers instead
8500 of what's specified by the `gnus-refer-thread-limit' variable."
8501   (interactive "P")
8502   (let ((id (mail-header-id (gnus-summary-article-header)))
8503         (limit (if limit (prefix-numeric-value limit)
8504                  gnus-refer-thread-limit)))
8505     (unless (eq gnus-fetch-old-headers 'invisible)
8506       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8507       ;; Retrieve the headers and read them in.
8508       (if (eq (if (numberp limit)
8509                   (gnus-retrieve-headers
8510                    (list (min
8511                           (+ (mail-header-number
8512                               (gnus-summary-article-header))
8513                              limit)
8514                           gnus-newsgroup-end))
8515                    gnus-newsgroup-name (* limit 2))
8516                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8517                 ;; headers.
8518                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8519                                        gnus-newsgroup-name limit))
8520               'nov)
8521           (gnus-build-all-threads)
8522         (error "Can't fetch thread from back ends that don't support NOV"))
8523       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8524     (gnus-summary-limit-include-thread id)))
8525
8526 (defun gnus-summary-refer-article (message-id)
8527   "Fetch an article specified by MESSAGE-ID."
8528   (interactive "sMessage-ID: ")
8529   (when (and (stringp message-id)
8530              (not (zerop (length message-id))))
8531     (setq message-id (gnus-replace-in-string message-id " " ""))
8532     ;; Construct the correct Message-ID if necessary.
8533     ;; Suggested by tale@pawl.rpi.edu.
8534     (unless (string-match "^<" message-id)
8535       (setq message-id (concat "<" message-id)))
8536     (unless (string-match ">$" message-id)
8537       (setq message-id (concat message-id ">")))
8538     ;; People often post MIDs from URLs, so unhex it:
8539     (unless (string-match "@" message-id)
8540       (setq message-id (gnus-url-unhex-string message-id)))
8541     (let* ((header (gnus-id-to-header message-id))
8542            (sparse (and header
8543                         (gnus-summary-article-sparse-p
8544                          (mail-header-number header))
8545                         (memq (mail-header-number header)
8546                               gnus-newsgroup-limit)))
8547            number)
8548       (cond
8549        ;; If the article is present in the buffer we just go to it.
8550        ((and header
8551              (or (not (gnus-summary-article-sparse-p
8552                        (mail-header-number header)))
8553                  sparse))
8554         (prog1
8555             (gnus-summary-goto-article
8556              (mail-header-number header) nil t)
8557           (when sparse
8558             (gnus-summary-update-article (mail-header-number header)))))
8559        (t
8560         ;; We fetch the article.
8561         (catch 'found
8562           (dolist (gnus-override-method (gnus-refer-article-methods))
8563             (when (and (gnus-check-server gnus-override-method)
8564                        ;; Fetch the header,
8565                        (setq number (gnus-summary-insert-subject message-id)))
8566               ;; and display the article.
8567               (gnus-summary-select-article nil nil nil number)
8568               (throw 'found t)))
8569           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8570
8571 (defun gnus-refer-article-methods ()
8572   "Return a list of referable methods."
8573   (cond
8574    ;; No method, so we default to current and native.
8575    ((null gnus-refer-article-method)
8576     (list gnus-current-select-method gnus-select-method))
8577    ;; Current.
8578    ((eq 'current gnus-refer-article-method)
8579     (list gnus-current-select-method))
8580    ;; List of select methods.
8581    ((not (and (symbolp (car gnus-refer-article-method))
8582               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8583     (let (out)
8584       (dolist (method gnus-refer-article-method)
8585         (push (if (eq 'current method)
8586                   gnus-current-select-method
8587                 method)
8588               out))
8589       (nreverse out)))
8590    ;; One single select method.
8591    (t
8592     (list gnus-refer-article-method))))
8593
8594 (defun gnus-summary-edit-parameters ()
8595   "Edit the group parameters of the current group."
8596   (interactive)
8597   (gnus-group-edit-group gnus-newsgroup-name 'params))
8598
8599 (defun gnus-summary-customize-parameters ()
8600   "Customize the group parameters of the current group."
8601   (interactive)
8602   (gnus-group-customize gnus-newsgroup-name))
8603
8604 (defun gnus-summary-enter-digest-group (&optional force)
8605   "Enter an nndoc group based on the current article.
8606 If FORCE, force a digest interpretation.  If not, try
8607 to guess what the document format is."
8608   (interactive "P")
8609   (let ((conf gnus-current-window-configuration))
8610     (save-window-excursion
8611       (save-excursion
8612         (let (gnus-article-prepare-hook
8613               gnus-display-mime-function
8614               gnus-break-pages)
8615           (gnus-summary-select-article))))
8616     (setq gnus-current-window-configuration conf)
8617     (let* ((name (format "%s-%d"
8618                          (gnus-group-prefixed-name
8619                           gnus-newsgroup-name (list 'nndoc ""))
8620                          (with-current-buffer gnus-summary-buffer
8621                            gnus-current-article)))
8622            (ogroup gnus-newsgroup-name)
8623            (params (append (gnus-info-params (gnus-get-info ogroup))
8624                            (list (cons 'to-group ogroup))
8625                            (list (cons 'parent-group ogroup))
8626                            (list (cons 'save-article-group ogroup))))
8627            (case-fold-search t)
8628            (buf (current-buffer))
8629            dig to-address)
8630       (save-excursion
8631         (set-buffer gnus-original-article-buffer)
8632         ;; Have the digest group inherit the main mail address of
8633         ;; the parent article.
8634         (when (setq to-address (or (gnus-fetch-field "reply-to")
8635                                    (gnus-fetch-field "from")))
8636           (setq params
8637                 (append
8638                  (list (cons 'to-address
8639                              (funcall gnus-decode-encoded-address-function
8640                                       to-address))))))
8641         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8642         (insert-buffer-substring gnus-original-article-buffer)
8643         ;; Remove lines that may lead nndoc to misinterpret the
8644         ;; document type.
8645         (narrow-to-region
8646          (goto-char (point-min))
8647          (or (search-forward "\n\n" nil t) (point)))
8648         (goto-char (point-min))
8649         (delete-matching-lines "^Path:\\|^From ")
8650         (widen))
8651       (unwind-protect
8652           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8653                     (gnus-newsgroup-ephemeral-ignored-charsets
8654                      gnus-newsgroup-ignored-charsets))
8655                 (gnus-group-read-ephemeral-group
8656                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8657                               (nndoc-article-type
8658                                ,(if force 'mbox 'guess)))
8659                  t nil nil nil
8660                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8661                                                         "ADAPT")))))
8662               ;; Make all postings to this group go to the parent group.
8663               (nconc (gnus-info-params (gnus-get-info name))
8664                      params)
8665             ;; Couldn't select this doc group.
8666             (switch-to-buffer buf)
8667             (gnus-set-global-variables)
8668             (gnus-configure-windows 'summary)
8669             (gnus-message 3 "Article couldn't be entered?"))
8670         (kill-buffer dig)))))
8671
8672 (defun gnus-summary-read-document (n)
8673   "Open a new group based on the current article(s).
8674 This will allow you to read digests and other similar
8675 documents as newsgroups.
8676 Obeys the standard process/prefix convention."
8677   (interactive "P")
8678   (let* ((ogroup gnus-newsgroup-name)
8679          (params (append (gnus-info-params (gnus-get-info ogroup))
8680                          (list (cons 'to-group ogroup))))
8681          group egroup groups vgroup)
8682     (dolist (article (gnus-summary-work-articles n))
8683       (setq group (format "%s-%d" gnus-newsgroup-name article))
8684       (gnus-summary-remove-process-mark article)
8685       (when (gnus-summary-display-article article)
8686         (save-excursion
8687           (with-temp-buffer
8688             (insert-buffer-substring gnus-original-article-buffer)
8689             ;; Remove some headers that may lead nndoc to make
8690             ;; the wrong guess.
8691             (message-narrow-to-head)
8692             (goto-char (point-min))
8693             (delete-matching-lines "^Path:\\|^From ")
8694             (widen)
8695             (if (setq egroup
8696                       (gnus-group-read-ephemeral-group
8697                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8698                                      (nndoc-article-type guess))
8699                        t nil t))
8700                 (progn
8701             ;; Make all postings to this group go to the parent group.
8702                   (nconc (gnus-info-params (gnus-get-info egroup))
8703                          params)
8704                   (push egroup groups))
8705               ;; Couldn't select this doc group.
8706               (gnus-error 3 "Article couldn't be entered"))))))
8707     ;; Now we have selected all the documents.
8708     (cond
8709      ((not groups)
8710       (error "None of the articles could be interpreted as documents"))
8711      ((gnus-group-read-ephemeral-group
8712        (setq vgroup (format
8713                      "nnvirtual:%s-%s" gnus-newsgroup-name
8714                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8715        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8716        t
8717        (cons (current-buffer) 'summary)))
8718      (t
8719       (error "Couldn't select virtual nndoc group")))))
8720
8721 (defun gnus-summary-isearch-article (&optional regexp-p)
8722   "Do incremental search forward on the current article.
8723 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8724   (interactive "P")
8725   (gnus-summary-select-article)
8726   (gnus-configure-windows 'article)
8727   (gnus-eval-in-buffer-window gnus-article-buffer
8728     (save-restriction
8729       (widen)
8730       (isearch-forward regexp-p))))
8731
8732 (defun gnus-summary-repeat-search-article-forward ()
8733   "Repeat the previous search forwards."
8734   (interactive)
8735   (unless gnus-last-search-regexp
8736     (error "No previous search"))
8737   (gnus-summary-search-article-forward gnus-last-search-regexp))
8738
8739 (defun gnus-summary-repeat-search-article-backward ()
8740   "Repeat the previous search backwards."
8741   (interactive)
8742   (unless gnus-last-search-regexp
8743     (error "No previous search"))
8744   (gnus-summary-search-article-forward gnus-last-search-regexp t))
8745
8746 (defun gnus-summary-search-article-forward (regexp &optional backward)
8747   "Search for an article containing REGEXP forward.
8748 If BACKWARD, search backward instead."
8749   (interactive
8750    (list (read-string
8751           (format "Search article %s (regexp%s): "
8752                   (if current-prefix-arg "backward" "forward")
8753                   (if gnus-last-search-regexp
8754                       (concat ", default " gnus-last-search-regexp)
8755                     "")))
8756          current-prefix-arg))
8757   (if (string-equal regexp "")
8758       (setq regexp (or gnus-last-search-regexp ""))
8759     (setq gnus-last-search-regexp regexp)
8760     (setq gnus-article-before-search gnus-current-article))
8761   ;; Intentionally set gnus-last-article.
8762   (setq gnus-last-article gnus-article-before-search)
8763   (let ((gnus-last-article gnus-last-article))
8764     (if (gnus-summary-search-article regexp backward)
8765         (gnus-summary-show-thread)
8766       (signal 'search-failed (list regexp)))))
8767
8768 (defun gnus-summary-search-article-backward (regexp)
8769   "Search for an article containing REGEXP backward."
8770   (interactive
8771    (list (read-string
8772           (format "Search article backward (regexp%s): "
8773                   (if gnus-last-search-regexp
8774                       (concat ", default " gnus-last-search-regexp)
8775                     "")))))
8776   (gnus-summary-search-article-forward regexp 'backward))
8777
8778 (defun gnus-summary-search-article (regexp &optional backward)
8779   "Search for an article containing REGEXP.
8780 Optional argument BACKWARD means do search for backward.
8781 `gnus-select-article-hook' is not called during the search."
8782   ;; We have to require this here to make sure that the following
8783   ;; dynamic binding isn't shadowed by autoloading.
8784   (require 'gnus-async)
8785   (require 'gnus-art)
8786   (let ((gnus-select-article-hook nil)  ;Disable hook.
8787         (gnus-article-prepare-hook nil)
8788         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8789         (gnus-use-article-prefetch nil)
8790         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8791         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8792         (gnus-visual nil)
8793         (gnus-keep-backlog nil)
8794         (gnus-break-pages nil)
8795         (gnus-summary-display-arrow nil)
8796         (gnus-updated-mode-lines nil)
8797         (gnus-auto-center-summary nil)
8798         (sum (current-buffer))
8799         (gnus-display-mime-function nil)
8800         (found nil)
8801         point)
8802     (gnus-save-hidden-threads
8803       (gnus-summary-select-article)
8804       (set-buffer gnus-article-buffer)
8805       (goto-char (window-point (get-buffer-window (current-buffer))))
8806       (when backward
8807         (forward-line -1))
8808       (while (not found)
8809         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8810         (if (if backward
8811                 (re-search-backward regexp nil t)
8812               (re-search-forward regexp nil t))
8813             ;; We found the regexp.
8814             (progn
8815               (setq found 'found)
8816               (beginning-of-line)
8817               (set-window-start
8818                (get-buffer-window (current-buffer))
8819                (point))
8820               (forward-line 1)
8821               (set-window-point
8822                (get-buffer-window (current-buffer))
8823                (point))
8824               (set-buffer sum)
8825               (setq point (point)))
8826           ;; We didn't find it, so we go to the next article.
8827           (set-buffer sum)
8828           (setq found 'not)
8829           (while (eq found 'not)
8830             (if (not (if backward (gnus-summary-find-prev)
8831                        (gnus-summary-find-next)))
8832                 ;; No more articles.
8833                 (setq found t)
8834               ;; Select the next article and adjust point.
8835               (unless (gnus-summary-article-sparse-p
8836                        (gnus-summary-article-number))
8837                 (setq found nil)
8838                 (gnus-summary-select-article)
8839                 (set-buffer gnus-article-buffer)
8840                 (widen)
8841                 (goto-char (if backward (point-max) (point-min))))))))
8842       (gnus-message 7 ""))
8843     ;; Return whether we found the regexp.
8844     (when (eq found 'found)
8845       (goto-char point)
8846       (gnus-summary-show-thread)
8847       (gnus-summary-goto-subject gnus-current-article)
8848       (gnus-summary-position-point)
8849       t)))
8850
8851 (defun gnus-find-matching-articles (header regexp)
8852   "Return a list of all articles that match REGEXP on HEADER.
8853 This search includes all articles in the current group that Gnus has
8854 fetched headers for, whether they are displayed or not."
8855   (let ((articles nil)
8856         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8857         (case-fold-search t))
8858     (dolist (header gnus-newsgroup-headers)
8859       (when (string-match regexp (funcall func header))
8860         (push (mail-header-number header) articles)))
8861     (nreverse articles)))
8862
8863 (defun gnus-summary-find-matching (header regexp &optional backward unread
8864                                           not-case-fold not-matching)
8865   "Return a list of all articles that match REGEXP on HEADER.
8866 The search stars on the current article and goes forwards unless
8867 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8868 If UNREAD is non-nil, only unread articles will
8869 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8870 in the comparisons. If NOT-MATCHING, return a list of all articles that
8871 not match REGEXP on HEADER."
8872   (let ((case-fold-search (not not-case-fold))
8873         articles d func)
8874     (if (consp header)
8875         (if (eq (car header) 'extra)
8876             (setq func
8877                   `(lambda (h)
8878                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8879                          "")))
8880           (error "%s is an invalid header" header))
8881       (unless (fboundp (intern (concat "mail-header-" header)))
8882         (error "%s is not a valid header" header))
8883       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8884     (dolist (d (if (eq backward 'all)
8885                    gnus-newsgroup-data
8886                  (gnus-data-find-list
8887                   (gnus-summary-article-number)
8888                   (gnus-data-list backward))))
8889       (when (and (or (not unread)       ; We want all articles...
8890                      (gnus-data-unread-p d)) ; Or just unreads.
8891                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8892                  (if not-matching
8893                      (not (string-match
8894                            regexp
8895                            (funcall func (gnus-data-header d))))
8896                    (string-match regexp
8897                                  (funcall func (gnus-data-header d)))))
8898         (push (gnus-data-number d) articles))) ; Success!
8899     (nreverse articles)))
8900
8901 (defun gnus-summary-execute-command (header regexp command &optional backward)
8902   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8903 If HEADER is an empty string (or nil), the match is done on the entire
8904 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8905   (interactive
8906    (list (let ((completion-ignore-case t))
8907            (completing-read
8908             "Header name: "
8909             (mapcar (lambda (header) (list (format "%s" header)))
8910                     (append
8911                      '("Number" "Subject" "From" "Lines" "Date"
8912                        "Message-ID" "Xref" "References" "Body")
8913                      gnus-extra-headers))
8914             nil 'require-match))
8915          (read-string "Regexp: ")
8916          (read-key-sequence "Command: ")
8917          current-prefix-arg))
8918   (when (equal header "Body")
8919     (setq header ""))
8920   ;; Hidden thread subtrees must be searched as well.
8921   (gnus-summary-show-all-threads)
8922   ;; We don't want to change current point nor window configuration.
8923   (save-excursion
8924     (save-window-excursion
8925       (let (gnus-visual
8926             gnus-treat-strip-trailing-blank-lines
8927             gnus-treat-strip-leading-blank-lines
8928             gnus-treat-strip-multiple-blank-lines
8929             gnus-treat-hide-boring-headers
8930             gnus-treat-fold-newsgroups
8931             gnus-article-prepare-hook)
8932         (gnus-message 6 "Executing %s..." (key-description command))
8933         ;; We'd like to execute COMMAND interactively so as to give arguments.
8934         (gnus-execute header regexp
8935                       `(call-interactively ',(key-binding command))
8936                       backward)
8937         (gnus-message 6 "Executing %s...done" (key-description command))))))
8938
8939 (defun gnus-summary-beginning-of-article ()
8940   "Scroll the article back to the beginning."
8941   (interactive)
8942   (gnus-summary-select-article)
8943   (gnus-configure-windows 'article)
8944   (gnus-eval-in-buffer-window gnus-article-buffer
8945     (widen)
8946     (goto-char (point-min))
8947     (when gnus-break-pages
8948       (gnus-narrow-to-page))))
8949
8950 (defun gnus-summary-end-of-article ()
8951   "Scroll to the end of the article."
8952   (interactive)
8953   (gnus-summary-select-article)
8954   (gnus-configure-windows 'article)
8955   (gnus-eval-in-buffer-window gnus-article-buffer
8956     (widen)
8957     (goto-char (point-max))
8958     (recenter -3)
8959     (when gnus-break-pages
8960       (when (re-search-backward page-delimiter nil t)
8961         (narrow-to-region (match-end 0) (point-max)))
8962       (gnus-narrow-to-page))))
8963
8964 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8965   "Truncate to LEN and quote all \"(\"'s in STRING."
8966   (gnus-replace-in-string (if (and len (> (length string) len))
8967                               (substring string 0 len)
8968                             string)
8969                           "[()]" "\\\\\\&"))
8970
8971 (defun gnus-summary-print-article (&optional filename n)
8972   "Generate and print a PostScript image of the process-marked (mail) articles.
8973
8974 If used interactively, print the current article if none are
8975 process-marked.  With prefix arg, prompt the user for the name of the
8976 file to save in.
8977
8978 When used from Lisp, accept two optional args FILENAME and N.  N means
8979 to print the next N articles.  If N is negative, print the N previous
8980 articles.  If N is nil and articles have been marked with the process
8981 mark, print these instead.
8982
8983 If the optional first argument FILENAME is nil, send the image to the
8984 printer.  If FILENAME is a string, save the PostScript image in a file with
8985 that name.  If FILENAME is a number, prompt the user for the name of the file
8986 to save in."
8987   (interactive (list (ps-print-preprint current-prefix-arg)))
8988   (dolist (article (gnus-summary-work-articles n))
8989     (gnus-summary-select-article nil nil 'pseudo article)
8990     (gnus-eval-in-buffer-window gnus-article-buffer
8991       (gnus-print-buffer))
8992     (gnus-summary-remove-process-mark article))
8993   (ps-despool filename))
8994
8995 (defun gnus-print-buffer ()
8996   (let ((buffer (generate-new-buffer " *print*")))
8997     (unwind-protect
8998         (progn
8999           (copy-to-buffer buffer (point-min) (point-max))
9000           (set-buffer buffer)
9001           (gnus-remove-text-with-property 'gnus-decoration)
9002           (when (gnus-visual-p 'article-highlight 'highlight)
9003             ;; Copy-to-buffer doesn't copy overlay.  So redo
9004             ;; highlight.
9005             (let ((gnus-article-buffer buffer))
9006               (gnus-article-highlight-citation t)
9007               (gnus-article-highlight-signature)
9008               (gnus-article-emphasize)
9009               (gnus-article-delete-invisible-text)))
9010           (let ((ps-left-header
9011                  (list
9012                   (concat "("
9013                           (gnus-summary-print-truncate-and-quote
9014                            (mail-header-subject gnus-current-headers)
9015                            66) ")")
9016                   (concat "("
9017                           (gnus-summary-print-truncate-and-quote
9018                            (mail-header-from gnus-current-headers)
9019                            45) ")")))
9020                 (ps-right-header
9021                  (list
9022                   "/pagenumberstring load"
9023                   (concat "("
9024                           (mail-header-date gnus-current-headers) ")"))))
9025             (gnus-run-hooks 'gnus-ps-print-hook)
9026             (save-excursion
9027               (if window-system
9028                   (ps-spool-buffer-with-faces)
9029                 (ps-spool-buffer)))))
9030       (kill-buffer buffer))))
9031
9032 (defun gnus-summary-show-article (&optional arg)
9033   "Force redisplaying of the current article.
9034 If ARG (the prefix) is a number, show the article with the charset
9035 defined in `gnus-summary-show-article-charset-alist', or the charset
9036 input.
9037 If ARG (the prefix) is non-nil and not a number, show the raw article
9038 without any article massaging functions being run.  Normally, the key
9039 strokes are `C-u g'."
9040   (interactive "P")
9041   (cond
9042    ((numberp arg)
9043     (gnus-summary-show-article t)
9044     (let ((gnus-newsgroup-charset
9045            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9046                (mm-read-coding-system
9047                 "View as charset: " ;; actually it is coding system.
9048                 (with-current-buffer gnus-article-buffer
9049                   (mm-detect-coding-region (point) (point-max))))))
9050           (gnus-newsgroup-ignored-charsets 'gnus-all))
9051       (gnus-summary-select-article nil 'force)
9052       (let ((deps gnus-newsgroup-dependencies)
9053             head header lines)
9054         (save-excursion
9055           (set-buffer gnus-original-article-buffer)
9056           (save-restriction
9057             (message-narrow-to-head)
9058             (setq head (buffer-string))
9059             (goto-char (point-min))
9060             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9061               (goto-char (point-max))
9062               (widen)
9063               (setq lines (1- (count-lines (point) (point-max))))))
9064           (with-temp-buffer
9065             (insert (format "211 %d Article retrieved.\n"
9066                             (cdr gnus-article-current)))
9067             (insert head)
9068             (if lines (insert (format "Lines: %d\n" lines)))
9069             (insert ".\n")
9070             (let ((nntp-server-buffer (current-buffer)))
9071               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9072         (gnus-data-set-header
9073          (gnus-data-find (cdr gnus-article-current))
9074          header)
9075         (gnus-summary-update-article-line
9076          (cdr gnus-article-current) header)
9077         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9078           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9079    ((not arg)
9080     ;; Select the article the normal way.
9081     (gnus-summary-select-article nil 'force))
9082    (t
9083     ;; We have to require this here to make sure that the following
9084     ;; dynamic binding isn't shadowed by autoloading.
9085     (require 'gnus-async)
9086     (require 'gnus-art)
9087     ;; Bind the article treatment functions to nil.
9088     (let ((gnus-have-all-headers t)
9089           gnus-article-prepare-hook
9090           gnus-article-decode-hook
9091           gnus-display-mime-function
9092           gnus-break-pages)
9093       ;; Destroy any MIME parts.
9094       (when (gnus-buffer-live-p gnus-article-buffer)
9095         (save-excursion
9096           (set-buffer gnus-article-buffer)
9097           (mm-destroy-parts gnus-article-mime-handles)
9098           ;; Set it to nil for safety reason.
9099           (setq gnus-article-mime-handle-alist nil)
9100           (setq gnus-article-mime-handles nil)))
9101       (gnus-summary-select-article nil 'force))))
9102   (gnus-summary-goto-subject gnus-current-article)
9103   (gnus-summary-position-point))
9104
9105 (defun gnus-summary-show-raw-article ()
9106   "Show the raw article without any article massaging functions being run."
9107   (interactive)
9108   (gnus-summary-show-article t))
9109
9110 (defun gnus-summary-verbose-headers (&optional arg)
9111   "Toggle permanent full header display.
9112 If ARG is a positive number, turn header display on.
9113 If ARG is a negative number, turn header display off."
9114   (interactive "P")
9115   (setq gnus-show-all-headers
9116         (cond ((or (not (numberp arg))
9117                    (zerop arg))
9118                (not gnus-show-all-headers))
9119               ((natnump arg)
9120                t)))
9121   (gnus-summary-show-article))
9122
9123 (defun gnus-summary-toggle-header (&optional arg)
9124   "Show the headers if they are hidden, or hide them if they are shown.
9125 If ARG is a positive number, show the entire header.
9126 If ARG is a negative number, hide the unwanted header lines."
9127   (interactive "P")
9128   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9129                      (get-buffer-window gnus-article-buffer t))))
9130     (with-current-buffer gnus-article-buffer
9131       (widen)
9132       (article-narrow-to-head)
9133       (let* ((buffer-read-only nil)
9134              (inhibit-point-motion-hooks t)
9135              (hidden (if (numberp arg)
9136                          (>= arg 0)
9137                        (or (not (looking-at "[^ \t\n]+:"))
9138                            (gnus-article-hidden-text-p 'headers))))
9139              s e)
9140         (delete-region (point-min) (point-max))
9141         (with-current-buffer gnus-original-article-buffer
9142           (goto-char (setq s (point-min)))
9143           (setq e (if (search-forward "\n\n" nil t)
9144                       (1- (point))
9145                     (point-max))))
9146         (insert-buffer-substring gnus-original-article-buffer s e)
9147         (run-hooks 'gnus-article-decode-hook)
9148         (if hidden
9149             (let ((gnus-treat-hide-headers nil)
9150                   (gnus-treat-hide-boring-headers nil))
9151               (gnus-delete-wash-type 'headers)
9152               (gnus-treat-article 'head))
9153           (gnus-treat-article 'head))
9154         (widen)
9155         (if window
9156             (set-window-start window (goto-char (point-min))))
9157         (if gnus-break-pages
9158             (gnus-narrow-to-page)
9159           (when (gnus-visual-p 'page-marker)
9160             (let ((buffer-read-only nil))
9161               (gnus-remove-text-with-property 'gnus-prev)
9162               (gnus-remove-text-with-property 'gnus-next))))
9163         (gnus-set-mode-line 'article)))))
9164
9165 (defun gnus-summary-show-all-headers ()
9166   "Make all header lines visible."
9167   (interactive)
9168   (gnus-summary-toggle-header 1))
9169
9170 (defun gnus-summary-caesar-message (&optional arg)
9171   "Caesar rotate the current article by 13.
9172 With a non-numerical prefix, also rotate headers.  A numerical
9173 prefix specifies how many places to rotate each letter forward."
9174   (interactive "P")
9175   (gnus-summary-select-article)
9176   (let ((mail-header-separator ""))
9177     (gnus-eval-in-buffer-window gnus-article-buffer
9178       (save-restriction
9179         (widen)
9180         (let ((start (window-start))
9181               buffer-read-only)
9182           (if (equal arg '(4))
9183               (message-caesar-buffer-body nil t)
9184             (message-caesar-buffer-body arg))
9185           (set-window-start (get-buffer-window (current-buffer)) start)))))
9186   ;; Create buttons and stuff...
9187   (gnus-treat-article nil))
9188
9189 (defun gnus-summary-idna-message (&optional arg)
9190   "Decode IDNA encoded domain names in the current articles.
9191 IDNA encoded domain names looks like `xn--bar'.  If a string
9192 remain unencoded after running this function, it is likely an
9193 invalid IDNA string (`xn--bar' is invalid).
9194
9195 You must have GNU Libidn (`http://www.gnu.org/software/libidn/')
9196 installed for this command to work."
9197   (interactive "P")
9198   (if (not (and (condition-case nil (require 'idna)
9199                   (file-error))
9200                 (mm-coding-system-p 'utf-8)
9201                 (executable-find (symbol-value 'idna-program))))
9202       (gnus-message
9203        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9204     (gnus-summary-select-article)
9205     (let ((mail-header-separator ""))
9206       (gnus-eval-in-buffer-window gnus-article-buffer
9207         (save-restriction
9208           (widen)
9209           (let ((start (window-start))
9210                 buffer-read-only)
9211             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9212               (replace-match (idna-to-unicode (match-string 1))))
9213             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9214
9215 (defun gnus-summary-morse-message (&optional arg)
9216   "Morse decode the current article."
9217   (interactive "P")
9218   (gnus-summary-select-article)
9219   (let ((mail-header-separator ""))
9220     (gnus-eval-in-buffer-window gnus-article-buffer
9221       (save-excursion
9222         (save-restriction
9223           (widen)
9224           (let ((pos (window-start))
9225                 buffer-read-only)
9226             (goto-char (point-min))
9227             (when (message-goto-body)
9228               (gnus-narrow-to-body))
9229             (goto-char (point-min))
9230             (while (search-forward "·" (point-max) t)
9231               (replace-match "."))
9232             (unmorse-region (point-min) (point-max))
9233             (widen)
9234             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9235
9236 (defun gnus-summary-stop-page-breaking ()
9237   "Stop page breaking in the current article."
9238   (interactive)
9239   (gnus-summary-select-article)
9240   (gnus-eval-in-buffer-window gnus-article-buffer
9241     (widen)
9242     (when (gnus-visual-p 'page-marker)
9243       (let ((buffer-read-only nil))
9244         (gnus-remove-text-with-property 'gnus-prev)
9245         (gnus-remove-text-with-property 'gnus-next))
9246       (setq gnus-page-broken nil))))
9247
9248 (defun gnus-summary-move-article (&optional n to-newsgroup
9249                                             select-method action)
9250   "Move the current article to a different newsgroup.
9251 If N is a positive number, move the N next articles.
9252 If N is a negative number, move the N previous articles.
9253 If N is nil and any articles have been marked with the process mark,
9254 move those articles instead.
9255 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9256 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9257 re-spool using this method.
9258
9259 When called interactively with TO-NEWSGROUP being nil, the value of
9260 the variable `gnus-move-split-methods' is used for finding a default
9261 for the target newsgroup.
9262
9263 For this function to work, both the current newsgroup and the
9264 newsgroup that you want to move to have to support the `request-move'
9265 and `request-accept' functions.
9266
9267 ACTION can be either `move' (the default), `crosspost' or `copy'."
9268   (interactive "P")
9269   (unless action
9270     (setq action 'move))
9271   ;; Check whether the source group supports the required functions.
9272   (cond ((and (eq action 'move)
9273               (not (gnus-check-backend-function
9274                     'request-move-article gnus-newsgroup-name)))
9275          (error "The current group does not support article moving"))
9276         ((and (eq action 'crosspost)
9277               (not (gnus-check-backend-function
9278                     'request-replace-article gnus-newsgroup-name)))
9279          (error "The current group does not support article editing")))
9280   (let ((articles (gnus-summary-work-articles n))
9281         (prefix (if (gnus-check-backend-function
9282                      'request-move-article gnus-newsgroup-name)
9283                     (funcall gnus-move-group-prefix-function
9284                              gnus-newsgroup-name)
9285                   ""))
9286         (names '((move "Move" "Moving")
9287                  (copy "Copy" "Copying")
9288                  (crosspost "Crosspost" "Crossposting")))
9289         (copy-buf (save-excursion
9290                     (nnheader-set-temp-buffer " *copy article*")))
9291         art-group to-method new-xref article to-groups articles-to-update-marks)
9292     (unless (assq action names)
9293       (error "Unknown action %s" action))
9294     ;; Read the newsgroup name.
9295     (when (and (not to-newsgroup)
9296                (not select-method))
9297       (if (and gnus-move-split-methods
9298                (not
9299                 (and (memq gnus-current-article articles)
9300                      (gnus-buffer-live-p gnus-original-article-buffer))))
9301           ;; When `gnus-move-split-methods' is non-nil, we have to
9302           ;; select an article to give `gnus-read-move-group-name' an
9303           ;; opportunity to suggest an appropriate default.  However,
9304           ;; we needn't render or mark the article.
9305           (let ((gnus-display-mime-function nil)
9306                 (gnus-article-prepare-hook nil)
9307                 (gnus-mark-article-hook nil))
9308             (gnus-summary-select-article nil nil nil (car articles))))
9309       (setq to-newsgroup
9310             (gnus-read-move-group-name
9311              (cadr (assq action names))
9312              (symbol-value (intern (format "gnus-current-%s-group" action)))
9313              articles prefix))
9314       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
9315     (setq to-method (or select-method
9316                         (gnus-server-to-method
9317                          (gnus-group-method to-newsgroup))))
9318     ;; Check the method we are to move this article to...
9319     (unless (gnus-check-backend-function
9320              'request-accept-article (car to-method))
9321       (error "%s does not support article copying" (car to-method)))
9322     (unless (gnus-check-server to-method)
9323       (error "Can't open server %s" (car to-method)))
9324     (gnus-message 6 "%s to %s: %s..."
9325                   (caddr (assq action names))
9326                   (or (car select-method) to-newsgroup) articles)
9327     (while articles
9328       (setq article (pop articles))
9329       (setq
9330        art-group
9331        (cond
9332         ;; Move the article.
9333         ((eq action 'move)
9334          ;; Remove this article from future suppression.
9335          (gnus-dup-unsuppress-article article)
9336          (let* ((from-method (gnus-find-method-for-group
9337                               gnus-newsgroup-name))
9338                 (to-method (gnus-find-method-for-group
9339                             to-newsgroup))
9340                 (move-is-internal (gnus-method-equal from-method to-method)))
9341          (gnus-request-move-article
9342           article                       ; Article to move
9343           gnus-newsgroup-name           ; From newsgroup
9344           (nth 1 (gnus-find-method-for-group
9345                   gnus-newsgroup-name)) ; Server
9346           (list 'gnus-request-accept-article
9347                 to-newsgroup (list 'quote select-method)
9348                 (not articles) t)       ; Accept form
9349           (not articles)                ; Only save nov last time
9350           move-is-internal)))           ; is this move internal?
9351         ;; Copy the article.
9352         ((eq action 'copy)
9353          (save-excursion
9354            (set-buffer copy-buf)
9355            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9356              (save-restriction
9357                (nnheader-narrow-to-headers)
9358                (dolist (hdr gnus-copy-article-ignored-headers)
9359                  (message-remove-header hdr t)))
9360              (gnus-request-accept-article
9361               to-newsgroup select-method (not articles) t))))
9362         ;; Crosspost the article.
9363         ((eq action 'crosspost)
9364          (let ((xref (message-tokenize-header
9365                       (mail-header-xref (gnus-summary-article-header article))
9366                       " ")))
9367            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9368                                   ":" (number-to-string article)))
9369            (unless xref
9370              (setq xref (list (system-name))))
9371            (setq new-xref
9372                  (concat
9373                   (mapconcat 'identity
9374                              (delete "Xref:" (delete new-xref xref))
9375                              " ")
9376                   " " new-xref))
9377            (save-excursion
9378              (set-buffer copy-buf)
9379              ;; First put the article in the destination group.
9380              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9381              (when (consp (setq art-group
9382                                 (gnus-request-accept-article
9383                                  to-newsgroup select-method (not articles))))
9384                (setq new-xref (concat new-xref " " (car art-group)
9385                                       ":"
9386                                       (number-to-string (cdr art-group))))
9387                ;; Now we have the new Xrefs header, so we insert
9388                ;; it and replace the new article.
9389                (nnheader-replace-header "Xref" new-xref)
9390                (gnus-request-replace-article
9391                 (cdr art-group) to-newsgroup (current-buffer))
9392                art-group))))))
9393       (cond
9394        ((not art-group)
9395         (gnus-message 1 "Couldn't %s article %s: %s"
9396                       (cadr (assq action names)) article
9397                       (nnheader-get-report (car to-method))))
9398        ((eq art-group 'junk)
9399         (when (eq action 'move)
9400           (gnus-summary-mark-article article gnus-canceled-mark)
9401           (gnus-message 4 "Deleted article %s" article)
9402           ;; run the delete hook
9403           (run-hook-with-args 'gnus-summary-article-delete-hook
9404                               action
9405                               (gnus-data-header
9406                                (assoc article (gnus-data-list nil)))
9407                               gnus-newsgroup-name nil
9408                               select-method)))
9409        (t
9410         (let* ((pto-group (gnus-group-prefixed-name
9411                            (car art-group) to-method))
9412                (info (gnus-get-info pto-group))
9413                (to-group (gnus-info-group info))
9414                to-marks)
9415           ;; Update the group that has been moved to.
9416           (when (and info
9417                      (memq action '(move copy)))
9418             (unless (member to-group to-groups)
9419               (push to-group to-groups))
9420
9421             (unless (memq article gnus-newsgroup-unreads)
9422               (push 'read to-marks)
9423               (gnus-info-set-read
9424                info (gnus-add-to-range (gnus-info-read info)
9425                                        (list (cdr art-group)))))
9426
9427             ;; See whether the article is to be put in the cache.
9428             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9429                              gnus-article-mark-lists
9430                            (delete '(expirable . expire)
9431                                    (copy-sequence gnus-article-mark-lists))))
9432                   (to-article (cdr art-group)))
9433
9434               ;; Enter the article into the cache in the new group,
9435               ;; if that is required.
9436               (when gnus-use-cache
9437                 (gnus-cache-possibly-enter-article
9438                  to-group to-article
9439                  (memq article gnus-newsgroup-marked)
9440                  (memq article gnus-newsgroup-dormant)
9441                  (memq article gnus-newsgroup-unreads)))
9442
9443               (when gnus-preserve-marks
9444                 ;; Copy any marks over to the new group.
9445                 (when (and (equal to-group gnus-newsgroup-name)
9446                            (not (memq article gnus-newsgroup-unreads)))
9447                   ;; Mark this article as read in this group.
9448                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9449                   (setcdr (gnus-active to-group) to-article)
9450                   (setcdr gnus-newsgroup-active to-article))
9451
9452                 (while marks
9453                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9454                     (when (memq article (symbol-value
9455                                          (intern (format "gnus-newsgroup-%s"
9456                                                          (caar marks)))))
9457                       (push (cdar marks) to-marks)
9458                       ;; If the other group is the same as this group,
9459                       ;; then we have to add the mark to the list.
9460                       (when (equal to-group gnus-newsgroup-name)
9461                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9462                              (cons to-article
9463                                    (symbol-value
9464                                     (intern (format "gnus-newsgroup-%s"
9465                                                     (caar marks)))))))
9466                       ;; Copy the marks to other group.
9467                       (gnus-add-marked-articles
9468                        to-group (cdar marks) (list to-article) info)))
9469                   (setq marks (cdr marks)))
9470
9471                 (gnus-request-set-mark
9472                  to-group (list (list (list to-article) 'add to-marks))))
9473
9474               (gnus-dribble-enter
9475                (concat "(gnus-group-set-info '"
9476                        (gnus-prin1-to-string (gnus-get-info to-group))
9477                        ")"))))
9478
9479           ;; Update the Xref header in this article to point to
9480           ;; the new crossposted article we have just created.
9481           (when (eq action 'crosspost)
9482             (save-excursion
9483               (set-buffer copy-buf)
9484               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9485               (nnheader-replace-header "Xref" new-xref)
9486               (gnus-request-replace-article
9487                article gnus-newsgroup-name (current-buffer))))
9488
9489           ;; run the move/copy/crosspost/respool hook
9490           (run-hook-with-args 'gnus-summary-article-move-hook
9491                               action
9492                               (gnus-data-header
9493                                (assoc article (gnus-data-list nil)))
9494                               gnus-newsgroup-name
9495                               to-newsgroup
9496                               select-method))
9497
9498         ;;;!!!Why is this necessary?
9499         (set-buffer gnus-summary-buffer)
9500         
9501         (gnus-summary-goto-subject article)
9502         (when (eq action 'move)
9503           (gnus-summary-mark-article article gnus-canceled-mark))))
9504       (push article articles-to-update-marks))
9505
9506     (apply 'gnus-summary-remove-process-mark articles-to-update-marks)
9507     ;; Re-activate all groups that have been moved to.
9508     (save-excursion
9509       (set-buffer gnus-group-buffer)
9510       (let ((gnus-group-marked to-groups))
9511         (gnus-group-get-new-news-this-group nil t)))
9512     
9513     (gnus-kill-buffer copy-buf)
9514     (gnus-summary-position-point)
9515     (gnus-set-mode-line 'summary)))
9516
9517 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9518   "Copy the current article to some other group.
9519 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9520 When called interactively, if TO-NEWSGROUP is nil, use the value of
9521 the variable `gnus-move-split-methods' for finding a default target
9522 newsgroup.
9523 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9524 re-spool using this method."
9525   (interactive "P")
9526   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9527
9528 (defun gnus-summary-crosspost-article (&optional n)
9529   "Crosspost the current article to some other group."
9530   (interactive "P")
9531   (gnus-summary-move-article n nil nil 'crosspost))
9532
9533 (defcustom gnus-summary-respool-default-method nil
9534   "Default method type for respooling an article.
9535 If nil, use to the current newsgroup method."
9536   :type 'symbol
9537   :group 'gnus-summary-mail)
9538
9539 (defcustom gnus-summary-display-while-building nil
9540   "If non-nil, show and update the summary buffer as it's being built.
9541 If the value is t, update the buffer after every line is inserted.  If
9542 the value is an integer (N), update the display every N lines."
9543   :version "22.1"
9544   :group 'gnus-thread
9545   :type '(choice (const :tag "off" nil)
9546                  number
9547                  (const :tag "frequently" t)))
9548
9549 (defun gnus-summary-respool-article (&optional n method)
9550   "Respool the current article.
9551 The article will be squeezed through the mail spooling process again,
9552 which means that it will be put in some mail newsgroup or other
9553 depending on `nnmail-split-methods'.
9554 If N is a positive number, respool the N next articles.
9555 If N is a negative number, respool the N previous articles.
9556 If N is nil and any articles have been marked with the process mark,
9557 respool those articles instead.
9558
9559 Respooling can be done both from mail groups and \"real\" newsgroups.
9560 In the former case, the articles in question will be moved from the
9561 current group into whatever groups they are destined to.  In the
9562 latter case, they will be copied into the relevant groups."
9563   (interactive
9564    (list current-prefix-arg
9565          (let* ((methods (gnus-methods-using 'respool))
9566                 (methname
9567                  (symbol-name (or gnus-summary-respool-default-method
9568                                   (car (gnus-find-method-for-group
9569                                         gnus-newsgroup-name)))))
9570                 (method
9571                  (gnus-completing-read-with-default
9572                   methname "Backend to use when respooling"
9573                   methods nil t nil 'gnus-mail-method-history))
9574                 ms)
9575            (cond
9576             ((zerop (length (setq ms (gnus-servers-using-backend
9577                                       (intern method)))))
9578              (list (intern method) ""))
9579             ((= 1 (length ms))
9580              (car ms))
9581             (t
9582              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9583                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9584                            ms-alist))))))))
9585   (unless method
9586     (error "No method given for respooling"))
9587   (if (assoc (symbol-name
9588               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9589              (gnus-methods-using 'respool))
9590       (gnus-summary-move-article n nil method)
9591     (gnus-summary-copy-article n nil method)))
9592
9593 (defun gnus-summary-import-article (file &optional edit)
9594   "Import an arbitrary file into a mail newsgroup."
9595   (interactive "fImport file: \nP")
9596   (let ((group gnus-newsgroup-name)
9597         (now (current-time))
9598         atts lines group-art)
9599     (unless (gnus-check-backend-function 'request-accept-article group)
9600       (error "%s does not support article importing" group))
9601     (or (file-readable-p file)
9602         (not (file-regular-p file))
9603         (error "Can't read %s" file))
9604     (save-excursion
9605       (set-buffer (gnus-get-buffer-create " *import file*"))
9606       (erase-buffer)
9607       (nnheader-insert-file-contents file)
9608       (goto-char (point-min))
9609       (if (nnheader-article-p)
9610           (save-restriction
9611             (goto-char (point-min))
9612             (search-forward "\n\n" nil t)
9613             (narrow-to-region (point-min) (1- (point)))
9614             (goto-char (point-min))
9615             (unless (re-search-forward "^date:" nil t)
9616               (goto-char (point-max))
9617               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9618        ;; This doesn't look like an article, so we fudge some headers.
9619         (setq atts (file-attributes file)
9620               lines (count-lines (point-min) (point-max)))
9621         (insert "From: " (read-string "From: ") "\n"
9622                 "Subject: " (read-string "Subject: ") "\n"
9623                 "Date: " (message-make-date (nth 5 atts)) "\n"
9624                 "Message-ID: " (message-make-message-id) "\n"
9625                 "Lines: " (int-to-string lines) "\n"
9626                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9627       (setq group-art (gnus-request-accept-article group nil t))
9628       (kill-buffer (current-buffer)))
9629     (setq gnus-newsgroup-active (gnus-activate-group group))
9630     (forward-line 1)
9631     (gnus-summary-goto-article (cdr group-art) nil t)
9632     (when edit
9633       (gnus-summary-edit-article))))
9634
9635 (defun gnus-summary-create-article ()
9636   "Create an article in a mail newsgroup."
9637   (interactive)
9638   (let ((group gnus-newsgroup-name)
9639         (now (current-time))
9640         group-art)
9641     (unless (gnus-check-backend-function 'request-accept-article group)
9642       (error "%s does not support article importing" group))
9643     (save-excursion
9644       (set-buffer (gnus-get-buffer-create " *import file*"))
9645       (erase-buffer)
9646       (goto-char (point-min))
9647       ;; This doesn't look like an article, so we fudge some headers.
9648       (insert "From: " (read-string "From: ") "\n"
9649               "Subject: " (read-string "Subject: ") "\n"
9650               "Date: " (message-make-date now) "\n"
9651               "Message-ID: " (message-make-message-id) "\n")
9652       (setq group-art (gnus-request-accept-article group nil t))
9653       (kill-buffer (current-buffer)))
9654     (setq gnus-newsgroup-active (gnus-activate-group group))
9655     (forward-line 1)
9656     (gnus-summary-goto-article (cdr group-art) nil t)
9657     (gnus-summary-edit-article)))
9658
9659 (defun gnus-summary-article-posted-p ()
9660   "Say whether the current (mail) article is available from news as well.
9661 This will be the case if the article has both been mailed and posted."
9662   (interactive)
9663   (let ((id (mail-header-references (gnus-summary-article-header)))
9664         (gnus-override-method (car (gnus-refer-article-methods))))
9665     (if (gnus-request-head id "")
9666         (gnus-message 2 "The current message was found on %s"
9667                       gnus-override-method)
9668       (gnus-message 2 "The current message couldn't be found on %s"
9669                     gnus-override-method)
9670       nil)))
9671
9672 (defun gnus-summary-expire-articles (&optional now)
9673   "Expire all articles that are marked as expirable in the current group."
9674   (interactive)
9675   (when (and (not gnus-group-is-exiting-without-update-p)
9676              (gnus-check-backend-function
9677               'request-expire-articles gnus-newsgroup-name))
9678     ;; This backend supports expiry.
9679     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9680            (expirable (if total
9681                           (progn
9682                             ;; We need to update the info for
9683                             ;; this group for `gnus-list-of-read-articles'
9684                             ;; to give us the right answer.
9685                             (gnus-run-hooks 'gnus-exit-group-hook)
9686                             (gnus-summary-update-info)
9687                             (gnus-list-of-read-articles gnus-newsgroup-name))
9688                         (setq gnus-newsgroup-expirable
9689                               (sort gnus-newsgroup-expirable '<))))
9690            (expiry-wait (if now 'immediate
9691                           (gnus-group-find-parameter
9692                            gnus-newsgroup-name 'expiry-wait)))
9693            (nnmail-expiry-target
9694             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9695                 nnmail-expiry-target))
9696            es)
9697       (when expirable
9698         ;; There are expirable articles in this group, so we run them
9699         ;; through the expiry process.
9700         (gnus-message 6 "Expiring articles...")
9701         (unless (gnus-check-group gnus-newsgroup-name)
9702           (error "Can't open server for %s" gnus-newsgroup-name))
9703         ;; The list of articles that weren't expired is returned.
9704         (save-excursion
9705           (if expiry-wait
9706               (let ((nnmail-expiry-wait-function nil)
9707                     (nnmail-expiry-wait expiry-wait))
9708                 (setq es (gnus-request-expire-articles
9709                           expirable gnus-newsgroup-name)))
9710             (setq es (gnus-request-expire-articles
9711                       expirable gnus-newsgroup-name)))
9712           (unless total
9713             (setq gnus-newsgroup-expirable es))
9714           ;; We go through the old list of expirable, and mark all
9715           ;; really expired articles as nonexistent.
9716           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9717             (let ((gnus-use-cache nil))
9718               (dolist (article expirable)
9719                 (when (and (not (memq article es))
9720                            (gnus-data-find article))
9721                   (gnus-summary-mark-article article gnus-canceled-mark)
9722                   (run-hook-with-args 'gnus-summary-article-expire-hook
9723                                       'delete
9724                                       (gnus-data-header
9725                                        (assoc article (gnus-data-list nil)))
9726                                       gnus-newsgroup-name
9727                                       nil
9728                                       nil))))))
9729         (gnus-message 6 "Expiring articles...done")))))
9730
9731 (defun gnus-summary-expire-articles-now ()
9732   "Expunge all expirable articles in the current group.
9733 This means that *all* articles that are marked as expirable will be
9734 deleted forever, right now."
9735   (interactive)
9736   (or gnus-expert-user
9737       (gnus-yes-or-no-p
9738        "Are you really, really sure you want to delete all expirable messages? ")
9739       (error "Phew!"))
9740   (gnus-summary-expire-articles t))
9741
9742 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9743 (defun gnus-summary-delete-article (&optional n)
9744   "Delete the N next (mail) articles.
9745 This command actually deletes articles.  This is not a marking
9746 command.  The article will disappear forever from your life, never to
9747 return.
9748
9749 If N is negative, delete backwards.
9750 If N is nil and articles have been marked with the process mark,
9751 delete these instead.
9752
9753 If `gnus-novice-user' is non-nil you will be asked for
9754 confirmation before the articles are deleted."
9755   (interactive "P")
9756   (unless (gnus-check-backend-function 'request-expire-articles
9757                                        gnus-newsgroup-name)
9758     (error "The current newsgroup does not support article deletion"))
9759   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9760     (error "Couldn't open server"))
9761   ;; Compute the list of articles to delete.
9762   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9763         (nnmail-expiry-target 'delete)
9764         not-deleted)
9765     (if (and gnus-novice-user
9766              (not (gnus-yes-or-no-p
9767                    (format "Do you really want to delete %s forever? "
9768                            (if (> (length articles) 1)
9769                                (format "these %s articles" (length articles))
9770                              "this article")))))
9771         ()
9772       ;; Delete the articles.
9773       (setq not-deleted (gnus-request-expire-articles
9774                          articles gnus-newsgroup-name 'force))
9775       (while articles
9776         (gnus-summary-remove-process-mark (car articles))
9777         ;; The backend might not have been able to delete the article
9778         ;; after all.
9779         (unless (memq (car articles) not-deleted)
9780           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9781         (let* ((article (car articles))
9782                (ghead  (gnus-data-header
9783                                     (assoc article (gnus-data-list nil)))))
9784           (run-hook-with-args 'gnus-summary-article-delete-hook
9785                               'delete ghead gnus-newsgroup-name nil
9786                               nil))
9787         (setq articles (cdr articles)))
9788       (when not-deleted
9789         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9790     (gnus-summary-position-point)
9791     (gnus-set-mode-line 'summary)
9792     not-deleted))
9793
9794 (defun gnus-summary-edit-article (&optional arg)
9795   "Edit the current article.
9796 This will have permanent effect only in mail groups.
9797 If ARG is nil, edit the decoded articles.
9798 If ARG is 1, edit the raw articles.
9799 If ARG is 2, edit the raw articles even in read-only groups.
9800 If ARG is 3, edit the articles with the current handles.
9801 Otherwise, allow editing of articles even in read-only
9802 groups."
9803   (interactive "P")
9804   (let (force raw current-handles)
9805     (cond
9806      ((null arg))
9807      ((eq arg 1)
9808       (setq raw t))
9809      ((eq arg 2)
9810       (setq raw t
9811             force t))
9812      ((eq arg 3)
9813       (setq current-handles
9814             (and (gnus-buffer-live-p gnus-article-buffer)
9815                  (with-current-buffer gnus-article-buffer
9816                    (prog1
9817                        gnus-article-mime-handles
9818                      (setq gnus-article-mime-handles nil))))))
9819      (t
9820       (setq force t)))
9821     (when (and raw (not force)
9822                (member gnus-newsgroup-name '("nndraft:delayed"
9823                                              "nndraft:drafts"
9824                                              "nndraft:queue")))
9825       (error "Can't edit the raw article in group %s"
9826              gnus-newsgroup-name))
9827     (save-excursion
9828       (set-buffer gnus-summary-buffer)
9829       (let ((mail-parse-charset gnus-newsgroup-charset)
9830             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9831         (gnus-set-global-variables)
9832         (when (and (not force)
9833                    (gnus-group-read-only-p))
9834           (error "The current newsgroup does not support article editing"))
9835         (gnus-summary-show-article t)
9836         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9837           (with-current-buffer gnus-article-buffer
9838             (mm-enable-multibyte)))
9839         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9840             (setq raw t))
9841         (gnus-article-edit-article
9842          (if raw 'ignore
9843            `(lambda ()
9844               (let ((mbl mml-buffer-list))
9845                 (setq mml-buffer-list nil)
9846                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
9847                   (mime-to-mml ,'current-handles))
9848                 (let ((mbl1 mml-buffer-list))
9849                   (setq mml-buffer-list mbl)
9850                   (set (make-local-variable 'mml-buffer-list) mbl1))
9851                 (gnus-make-local-hook 'kill-buffer-hook)
9852                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9853          `(lambda (no-highlight)
9854             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9855                   (message-options message-options)
9856                   (message-options-set-recipient)
9857                   (mail-parse-ignored-charsets
9858                    ',gnus-newsgroup-ignored-charsets))
9859               ,(if (not raw) '(progn
9860                                 (mml-to-mime)
9861                                 (mml-destroy-buffers)
9862                                 (remove-hook 'kill-buffer-hook
9863                                              'mml-destroy-buffers t)
9864                                 (kill-local-variable 'mml-buffer-list)))
9865               (gnus-summary-edit-article-done
9866                ,(or (mail-header-references gnus-current-headers) "")
9867                ,(gnus-group-read-only-p)
9868                ,gnus-summary-buffer no-highlight))))))))
9869
9870 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9871
9872 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9873                                                  no-highlight)
9874   "Make edits to the current article permanent."
9875   (interactive)
9876   (save-excursion
9877    ;; The buffer restriction contains the entire article if it exists.
9878     (when (article-goto-body)
9879       (let ((lines (count-lines (point) (point-max)))
9880             (length (- (point-max) (point)))
9881             (case-fold-search t)
9882             (body (copy-marker (point))))
9883         (goto-char (point-min))
9884         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9885           (delete-region (match-beginning 1) (match-end 1))
9886           (insert (number-to-string length)))
9887         (goto-char (point-min))
9888         (when (re-search-forward
9889                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9890           (delete-region (match-beginning 1) (match-end 1))
9891           (insert (number-to-string length)))
9892         (goto-char (point-min))
9893         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9894           (delete-region (match-beginning 1) (match-end 1))
9895           (insert (number-to-string lines))))))
9896   ;; Replace the article.
9897   (let ((buf (current-buffer)))
9898     (with-temp-buffer
9899       (insert-buffer-substring buf)
9900
9901       (if (and (not read-only)
9902                (not (gnus-request-replace-article
9903                      (cdr gnus-article-current) (car gnus-article-current)
9904                      (current-buffer) t)))
9905           (error "Couldn't replace article")
9906         ;; Update the summary buffer.
9907         (if (and references
9908                  (equal (message-tokenize-header references " ")
9909                         (message-tokenize-header
9910                          (or (message-fetch-field "references") "") " ")))
9911             ;; We only have to update this line.
9912             (save-excursion
9913               (save-restriction
9914                 (message-narrow-to-head)
9915                 (let ((head (buffer-substring-no-properties
9916                              (point-min) (point-max)))
9917                       header)
9918                   (with-temp-buffer
9919                     (insert (format "211 %d Article retrieved.\n"
9920                                     (cdr gnus-article-current)))
9921                     (insert head)
9922                     (insert ".\n")
9923                     (let ((nntp-server-buffer (current-buffer)))
9924                       (setq header (car (gnus-get-newsgroup-headers
9925                                          nil t))))
9926                     (save-excursion
9927                       (set-buffer gnus-summary-buffer)
9928                       (gnus-data-set-header
9929                        (gnus-data-find (cdr gnus-article-current))
9930                        header)
9931                       (gnus-summary-update-article-line
9932                        (cdr gnus-article-current) header)
9933                       (if (gnus-summary-goto-subject
9934                            (cdr gnus-article-current) nil t)
9935                           (gnus-summary-update-secondary-mark
9936                            (cdr gnus-article-current))))))))
9937           ;; Update threads.
9938           (set-buffer (or buffer gnus-summary-buffer))
9939           (gnus-summary-update-article (cdr gnus-article-current))
9940           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9941               (gnus-summary-update-secondary-mark
9942                (cdr gnus-article-current))))
9943         ;; Prettify the article buffer again.
9944         (unless no-highlight
9945           (save-excursion
9946             (set-buffer gnus-article-buffer)
9947             ;;;!!! Fix this -- article should be rehighlighted.
9948             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9949             (set-buffer gnus-original-article-buffer)
9950             (gnus-request-article
9951              (cdr gnus-article-current)
9952              (car gnus-article-current) (current-buffer))))
9953         ;; Prettify the summary buffer line.
9954         (when (gnus-visual-p 'summary-highlight 'highlight)
9955           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9956
9957 (defun gnus-summary-edit-wash (key)
9958   "Perform editing command KEY in the article buffer."
9959   (interactive
9960    (list
9961     (progn
9962       (message "%s" (concat (this-command-keys) "- "))
9963       (read-char))))
9964   (message "")
9965   (gnus-summary-edit-article)
9966   (execute-kbd-macro (concat (this-command-keys) key))
9967   (gnus-article-edit-done))
9968
9969 ;;; Respooling
9970
9971 (defun gnus-summary-respool-query (&optional silent trace)
9972   "Query where the respool algorithm would put this article."
9973   (interactive)
9974   (let (gnus-mark-article-hook)
9975     (gnus-summary-select-article)
9976     (save-excursion
9977       (set-buffer gnus-original-article-buffer)
9978       (let ((groups (nnmail-article-group 'identity trace)))
9979         (unless silent
9980           (if groups
9981               (message "This message would go to %s"
9982                        (mapconcat 'car groups ", "))
9983             (message "This message would go to no groups"))
9984           groups)))))
9985
9986 (defun gnus-summary-respool-trace ()
9987   "Trace where the respool algorithm would put this article.
9988 Display a buffer showing all fancy splitting patterns which matched."
9989   (interactive)
9990   (gnus-summary-respool-query nil t))
9991
9992 ;; Summary marking commands.
9993
9994 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9995   "Mark articles which has the same subject as read, and then select the next.
9996 If UNMARK is positive, remove any kind of mark.
9997 If UNMARK is negative, tick articles."
9998   (interactive "P")
9999   (when unmark
10000     (setq unmark (prefix-numeric-value unmark)))
10001   (let ((count
10002          (gnus-summary-mark-same-subject
10003           (gnus-summary-article-subject) unmark)))
10004     ;; Select next unread article.  If auto-select-same mode, should
10005     ;; select the first unread article.
10006     (gnus-summary-next-article t (and gnus-auto-select-same
10007                                       (gnus-summary-article-subject)))
10008     (gnus-message 7 "%d article%s marked as %s"
10009                   count (if (= count 1) " is" "s are")
10010                   (if unmark "unread" "read"))))
10011
10012 (defun gnus-summary-kill-same-subject (&optional unmark)
10013   "Mark articles which has the same subject as read.
10014 If UNMARK is positive, remove any kind of mark.
10015 If UNMARK is negative, tick articles."
10016   (interactive "P")
10017   (when unmark
10018     (setq unmark (prefix-numeric-value unmark)))
10019   (let ((count
10020          (gnus-summary-mark-same-subject
10021           (gnus-summary-article-subject) unmark)))
10022     ;; If marked as read, go to next unread subject.
10023     (when (null unmark)
10024       ;; Go to next unread subject.
10025       (gnus-summary-next-subject 1 t))
10026     (gnus-message 7 "%d articles are marked as %s"
10027                   count (if unmark "unread" "read"))))
10028
10029 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10030   "Mark articles with same SUBJECT as read, and return marked number.
10031 If optional argument UNMARK is positive, remove any kinds of marks.
10032 If optional argument UNMARK is negative, mark articles as unread instead."
10033   (let ((count 1))
10034     (save-excursion
10035       (cond
10036        ((null unmark)                   ; Mark as read.
10037         (while (and
10038                 (progn
10039                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10040                   (gnus-summary-show-thread) t)
10041                 (gnus-summary-find-subject subject))
10042           (setq count (1+ count))))
10043        ((> unmark 0)                    ; Tick.
10044         (while (and
10045                 (progn
10046                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10047                   (gnus-summary-show-thread) t)
10048                 (gnus-summary-find-subject subject))
10049           (setq count (1+ count))))
10050        (t                               ; Mark as unread.
10051         (while (and
10052                 (progn
10053                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10054                   (gnus-summary-show-thread) t)
10055                 (gnus-summary-find-subject subject))
10056           (setq count (1+ count)))))
10057       (gnus-set-mode-line 'summary)
10058       ;; Return the number of marked articles.
10059       count)))
10060
10061 (defun gnus-summary-mark-as-processable (n &optional unmark)
10062   "Set the process mark on the next N articles.
10063 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10064 the process mark instead.  The difference between N and the actual
10065 number of articles marked is returned."
10066   (interactive "P")
10067   (if (and (null n) (gnus-region-active-p))
10068       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10069     (setq n (prefix-numeric-value n))
10070     (let ((backward (< n 0))
10071           (n (abs n)))
10072       (while (and
10073               (> n 0)
10074               (if unmark
10075                   (gnus-summary-remove-process-mark
10076                    (gnus-summary-article-number))
10077                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10078               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10079         (setq n (1- n)))
10080       (when (/= 0 n)
10081         (gnus-message 7 "No more articles"))
10082       (gnus-summary-recenter)
10083       (gnus-summary-position-point)
10084       n)))
10085
10086 (defun gnus-summary-unmark-as-processable (n)
10087   "Remove the process mark from the next N articles.
10088 If N is negative, unmark backward instead.  The difference between N and
10089 the actual number of articles unmarked is returned."
10090   (interactive "P")
10091   (gnus-summary-mark-as-processable n t))
10092
10093 (defun gnus-summary-unmark-all-processable ()
10094   "Remove the process mark from all articles."
10095   (interactive)
10096   (save-excursion
10097     (while gnus-newsgroup-processable
10098       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10099   (gnus-summary-position-point))
10100
10101 (defun gnus-summary-add-mark (article type)
10102   "Mark ARTICLE with a mark of TYPE."
10103   (let ((vtype (car (assq type gnus-article-mark-lists)))
10104         var)
10105     (if (not vtype)
10106         (error "No such mark type: %s" type)
10107       (setq var (intern (format "gnus-newsgroup-%s" type)))
10108       (set var (cons article (symbol-value var)))
10109       (if (memq type '(processable cached replied forwarded recent saved))
10110           (gnus-summary-update-secondary-mark article)
10111         ;;; !!! This is bogus.  We should find out what primary
10112         ;;; !!! mark we want to set.
10113         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10114
10115 (defun gnus-summary-mark-as-expirable (n)
10116   "Mark N articles forward as expirable.
10117 If N is negative, mark backward instead.  The difference between N and
10118 the actual number of articles marked is returned."
10119   (interactive "p")
10120   (gnus-summary-mark-forward n gnus-expirable-mark))
10121
10122 (defun gnus-summary-mark-as-spam (n)
10123   "Mark N articles forward as spam.
10124 If N is negative, mark backward instead.  The difference between N and
10125 the actual number of articles marked is returned."
10126   (interactive "p")
10127   (gnus-summary-mark-forward n gnus-spam-mark))
10128
10129 (defun gnus-summary-mark-article-as-replied (article)
10130   "Mark ARTICLE as replied to and update the summary line.
10131 ARTICLE can also be a list of articles."
10132   (interactive (list (gnus-summary-article-number)))
10133   (let ((articles (if (listp article) article (list article))))
10134     (dolist (article articles)
10135       (unless (numberp article)
10136         (error "%s is not a number" article))
10137       (push article gnus-newsgroup-replied)
10138       (let ((buffer-read-only nil))
10139         (when (gnus-summary-goto-subject article nil t)
10140           (gnus-summary-update-secondary-mark article))))))
10141
10142 (defun gnus-summary-mark-article-as-forwarded (article)
10143   "Mark ARTICLE as forwarded and update the summary line.
10144 ARTICLE can also be a list of articles."
10145   (let ((articles (if (listp article) article (list article))))
10146     (dolist (article articles)
10147       (push article gnus-newsgroup-forwarded)
10148       (let ((buffer-read-only nil))
10149         (when (gnus-summary-goto-subject article nil t)
10150           (gnus-summary-update-secondary-mark article))))))
10151
10152 (defun gnus-summary-set-bookmark (article)
10153   "Set a bookmark in current article."
10154   (interactive (list (gnus-summary-article-number)))
10155   (when (or (not (get-buffer gnus-article-buffer))
10156             (not gnus-current-article)
10157             (not gnus-article-current)
10158             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10159     (error "No current article selected"))
10160   ;; Remove old bookmark, if one exists.
10161   (gnus-pull article gnus-newsgroup-bookmarks)
10162   ;; Set the new bookmark, which is on the form
10163   ;; (article-number . line-number-in-body).
10164   (push
10165    (cons article
10166          (with-current-buffer gnus-article-buffer
10167            (count-lines
10168             (min (point)
10169                  (save-excursion
10170                    (article-goto-body)
10171                    (point)))
10172             (point))))
10173    gnus-newsgroup-bookmarks)
10174   (gnus-message 6 "A bookmark has been added to the current article."))
10175
10176 (defun gnus-summary-remove-bookmark (article)
10177   "Remove the bookmark from the current article."
10178   (interactive (list (gnus-summary-article-number)))
10179   ;; Remove old bookmark, if one exists.
10180   (if (not (assq article gnus-newsgroup-bookmarks))
10181       (gnus-message 6 "No bookmark in current article.")
10182     (gnus-pull article gnus-newsgroup-bookmarks)
10183     (gnus-message 6 "Removed bookmark.")))
10184
10185 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10186 (defun gnus-summary-mark-as-dormant (n)
10187   "Mark N articles forward as dormant.
10188 If N is negative, mark backward instead.  The difference between N and
10189 the actual number of articles marked is returned."
10190   (interactive "p")
10191   (gnus-summary-mark-forward n gnus-dormant-mark))
10192
10193 (defun gnus-summary-set-process-mark (article)
10194   "Set the process mark on ARTICLE and update the summary line."
10195   (setq gnus-newsgroup-processable
10196         (cons article
10197               (delq article gnus-newsgroup-processable)))
10198   (when (gnus-summary-goto-subject article)
10199     (gnus-summary-show-thread)
10200     (gnus-summary-goto-subject article)
10201     (gnus-summary-update-secondary-mark article)))
10202
10203 (defun gnus-summary-remove-process-mark (&rest articles)
10204   "Remove the process mark from ARTICLES and update the summary line."
10205   (dolist (article articles)
10206     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10207     (when (gnus-summary-goto-subject article)
10208       (gnus-summary-show-thread)
10209       (gnus-summary-goto-subject article)
10210       (gnus-summary-update-secondary-mark article)))
10211   t)
10212
10213 (defun gnus-summary-set-saved-mark (article)
10214   "Set the process mark on ARTICLE and update the summary line."
10215   (push article gnus-newsgroup-saved)
10216   (when (gnus-summary-goto-subject article)
10217     (gnus-summary-update-secondary-mark article)))
10218
10219 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10220   "Mark N articles as read forwards.
10221 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10222 The difference between N and the actual number of articles marked is
10223 returned.
10224 If NO-EXPIRE, auto-expiry will be inhibited."
10225   (interactive "p")
10226   (gnus-summary-show-thread)
10227   (let ((backward (< n 0))
10228         (gnus-summary-goto-unread
10229          (and gnus-summary-goto-unread
10230               (not (eq gnus-summary-goto-unread 'never))
10231               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10232                                     gnus-ticked-mark gnus-dormant-mark)))))
10233         (n (abs n))
10234         (mark (or mark gnus-del-mark)))
10235     (while (and (> n 0)
10236                 (gnus-summary-mark-article nil mark no-expire)
10237                 (zerop (gnus-summary-next-subject
10238                         (if backward -1 1)
10239                         (and gnus-summary-goto-unread
10240                              (not (eq gnus-summary-goto-unread 'never)))
10241                         t)))
10242       (setq n (1- n)))
10243     (when (/= 0 n)
10244       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10245     (gnus-summary-recenter)
10246     (gnus-summary-position-point)
10247     (gnus-set-mode-line 'summary)
10248     n))
10249
10250 (defun gnus-summary-mark-article-as-read (mark)
10251   "Mark the current article quickly as read with MARK."
10252   (let ((article (gnus-summary-article-number)))
10253     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10254     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10255     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10256     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10257     (push (cons article mark) gnus-newsgroup-reads)
10258     ;; Possibly remove from cache, if that is used.
10259     (when gnus-use-cache
10260       (gnus-cache-enter-remove-article article))
10261     ;; Allow the backend to change the mark.
10262     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10263     ;; Check for auto-expiry.
10264     (when (and gnus-newsgroup-auto-expire
10265                (memq mark gnus-auto-expirable-marks))
10266       (setq mark gnus-expirable-mark)
10267       ;; Let the backend know about the mark change.
10268       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10269       (push article gnus-newsgroup-expirable))
10270     ;; Set the mark in the buffer.
10271     (gnus-summary-update-mark mark 'unread)
10272     t))
10273
10274 (defun gnus-summary-mark-article-as-unread (mark)
10275   "Mark the current article quickly as unread with MARK."
10276   (let* ((article (gnus-summary-article-number))
10277          (old-mark (gnus-summary-article-mark article)))
10278     ;; Allow the backend to change the mark.
10279     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10280     (if (eq mark old-mark)
10281         t
10282       (if (<= article 0)
10283           (progn
10284             (gnus-error 1 "Can't mark negative article numbers")
10285             nil)
10286         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10287         (setq gnus-newsgroup-spam-marked
10288               (delq article gnus-newsgroup-spam-marked))
10289         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10290         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10291         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10292         (cond ((= mark gnus-ticked-mark)
10293                (setq gnus-newsgroup-marked
10294                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10295                                               article)))
10296               ((= mark gnus-spam-mark)
10297                (setq gnus-newsgroup-spam-marked
10298                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10299                                               article)))
10300               ((= mark gnus-dormant-mark)
10301                (setq gnus-newsgroup-dormant
10302                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10303                                               article)))
10304               (t
10305                (setq gnus-newsgroup-unreads
10306                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10307                                               article))))
10308         (gnus-pull article gnus-newsgroup-reads)
10309
10310         ;; See whether the article is to be put in the cache.
10311         (and gnus-use-cache
10312              (vectorp (gnus-summary-article-header article))
10313              (save-excursion
10314                (gnus-cache-possibly-enter-article
10315                 gnus-newsgroup-name article
10316                 (= mark gnus-ticked-mark)
10317                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10318
10319         ;; Fix the mark.
10320         (gnus-summary-update-mark mark 'unread)
10321         t))))
10322
10323 (defun gnus-summary-mark-article (&optional article mark no-expire)
10324   "Mark ARTICLE with MARK.  MARK can be any character.
10325 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10326 `??' (dormant) and `?E' (expirable).
10327 If MARK is nil, then the default character `?r' is used.
10328 If ARTICLE is nil, then the article on the current line will be
10329 marked.
10330 If NO-EXPIRE, auto-expiry will be inhibited."
10331   ;; The mark might be a string.
10332   (when (stringp mark)
10333     (setq mark (aref mark 0)))
10334   ;; If no mark is given, then we check auto-expiring.
10335   (when (null mark)
10336     (setq mark gnus-del-mark))
10337   (when (and (not no-expire)
10338              gnus-newsgroup-auto-expire
10339              (memq mark gnus-auto-expirable-marks))
10340     (setq mark gnus-expirable-mark))
10341   (let ((article (or article (gnus-summary-article-number)))
10342         (old-mark (gnus-summary-article-mark article)))
10343     ;; Allow the backend to change the mark.
10344     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10345     (if (eq mark old-mark)
10346         t
10347       (unless article
10348         (error "No article on current line"))
10349       (if (not (if (or (= mark gnus-unread-mark)
10350                        (= mark gnus-ticked-mark)
10351                        (= mark gnus-spam-mark)
10352                        (= mark gnus-dormant-mark))
10353                    (gnus-mark-article-as-unread article mark)
10354                  (gnus-mark-article-as-read article mark)))
10355           t
10356         ;; See whether the article is to be put in the cache.
10357         (and gnus-use-cache
10358              (not (= mark gnus-canceled-mark))
10359              (vectorp (gnus-summary-article-header article))
10360              (save-excursion
10361                (gnus-cache-possibly-enter-article
10362                 gnus-newsgroup-name article
10363                 (= mark gnus-ticked-mark)
10364                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10365
10366         (when (gnus-summary-goto-subject article nil t)
10367           (let ((buffer-read-only nil))
10368             (gnus-summary-show-thread)
10369             ;; Fix the mark.
10370             (gnus-summary-update-mark mark 'unread)
10371             t))))))
10372
10373 (defun gnus-summary-update-secondary-mark (article)
10374   "Update the secondary (read, process, cache) mark."
10375   (gnus-summary-update-mark
10376    (cond ((memq article gnus-newsgroup-processable)
10377           gnus-process-mark)
10378          ((memq article gnus-newsgroup-cached)
10379           gnus-cached-mark)
10380          ((memq article gnus-newsgroup-replied)
10381           gnus-replied-mark)
10382          ((memq article gnus-newsgroup-forwarded)
10383           gnus-forwarded-mark)
10384          ((memq article gnus-newsgroup-saved)
10385           gnus-saved-mark)
10386          ((memq article gnus-newsgroup-recent)
10387           gnus-recent-mark)
10388          ((memq article gnus-newsgroup-unseen)
10389           gnus-unseen-mark)
10390          (t gnus-no-mark))
10391    'replied)
10392   (when (gnus-visual-p 'summary-highlight 'highlight)
10393     (gnus-run-hooks 'gnus-summary-update-hook))
10394   t)
10395
10396 (defun gnus-summary-update-download-mark (article)
10397   "Update the download mark."
10398   (gnus-summary-update-mark
10399    (cond ((memq article gnus-newsgroup-undownloaded)
10400           gnus-undownloaded-mark)
10401          (gnus-newsgroup-agentized
10402           gnus-downloaded-mark)
10403          (t
10404           gnus-no-mark))
10405    'download)
10406   (gnus-summary-update-line t)
10407   t)
10408
10409 (defun gnus-summary-update-mark (mark type)
10410   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10411         (buffer-read-only nil))
10412     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10413     (when forward
10414       (when (looking-at "\r")
10415         (incf forward))
10416       (when (<= (+ forward (point)) (point-max))
10417         ;; Go to the right position on the line.
10418         (goto-char (+ forward (point)))
10419         ;; Replace the old mark with the new mark.
10420         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10421         ;; Optionally update the marks by some user rule.
10422         (when (eq type 'unread)
10423           (gnus-data-set-mark
10424            (gnus-data-find (gnus-summary-article-number)) mark)
10425           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10426
10427 (defun gnus-mark-article-as-read (article &optional mark)
10428   "Enter ARTICLE in the pertinent lists and remove it from others."
10429   ;; Make the article expirable.
10430   (let ((mark (or mark gnus-del-mark)))
10431     (setq gnus-newsgroup-expirable
10432           (if (= mark gnus-expirable-mark)
10433               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10434             (delq article gnus-newsgroup-expirable)))
10435     ;; Remove from unread and marked lists.
10436     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10437     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10438     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10439     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10440     (push (cons article mark) gnus-newsgroup-reads)
10441     ;; Possibly remove from cache, if that is used.
10442     (when gnus-use-cache
10443       (gnus-cache-enter-remove-article article))
10444     t))
10445
10446 (defun gnus-mark-article-as-unread (article &optional mark)
10447   "Enter ARTICLE in the pertinent lists and remove it from others."
10448   (let ((mark (or mark gnus-ticked-mark)))
10449     (if (<= article 0)
10450         (progn
10451           (gnus-error 1 "Can't mark negative article numbers")
10452           nil)
10453       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10454             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10455             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10456             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10457             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10458
10459       ;; Unsuppress duplicates?
10460       (when gnus-suppress-duplicates
10461         (gnus-dup-unsuppress-article article))
10462
10463       (cond ((= mark gnus-ticked-mark)
10464              (setq gnus-newsgroup-marked
10465                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10466             ((= mark gnus-spam-mark)
10467              (setq gnus-newsgroup-spam-marked
10468                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10469                                             article)))
10470             ((= mark gnus-dormant-mark)
10471              (setq gnus-newsgroup-dormant
10472                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10473             (t
10474              (setq gnus-newsgroup-unreads
10475                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10476       (gnus-pull article gnus-newsgroup-reads)
10477       t)))
10478
10479 (defalias 'gnus-summary-mark-as-unread-forward
10480   'gnus-summary-tick-article-forward)
10481 (make-obsolete 'gnus-summary-mark-as-unread-forward
10482                'gnus-summary-tick-article-forward)
10483 (defun gnus-summary-tick-article-forward (n)
10484   "Tick N articles forwards.
10485 If N is negative, tick backwards instead.
10486 The difference between N and the number of articles ticked is returned."
10487   (interactive "p")
10488   (gnus-summary-mark-forward n gnus-ticked-mark))
10489
10490 (defalias 'gnus-summary-mark-as-unread-backward
10491   'gnus-summary-tick-article-backward)
10492 (make-obsolete 'gnus-summary-mark-as-unread-backward
10493                'gnus-summary-tick-article-backward)
10494 (defun gnus-summary-tick-article-backward (n)
10495   "Tick N articles backwards.
10496 The difference between N and the number of articles ticked is returned."
10497   (interactive "p")
10498   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10499
10500 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10501 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10502 (defun gnus-summary-tick-article (&optional article clear-mark)
10503   "Mark current article as unread.
10504 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10505 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10506   (interactive)
10507   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10508                                        gnus-ticked-mark)))
10509
10510 (defun gnus-summary-mark-as-read-forward (n)
10511   "Mark N articles as read forwards.
10512 If N is negative, mark backwards instead.
10513 The difference between N and the actual number of articles marked is
10514 returned."
10515   (interactive "p")
10516   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10517
10518 (defun gnus-summary-mark-as-read-backward (n)
10519   "Mark the N articles as read backwards.
10520 The difference between N and the actual number of articles marked is
10521 returned."
10522   (interactive "p")
10523   (gnus-summary-mark-forward
10524    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10525
10526 (defun gnus-summary-mark-as-read (&optional article mark)
10527   "Mark current article as read.
10528 ARTICLE specifies the article to be marked as read.
10529 MARK specifies a string to be inserted at the beginning of the line."
10530   (gnus-summary-mark-article article mark))
10531
10532 (defun gnus-summary-clear-mark-forward (n)
10533   "Clear marks from N articles forward.
10534 If N is negative, clear backward instead.
10535 The difference between N and the number of marks cleared is returned."
10536   (interactive "p")
10537   (gnus-summary-mark-forward n gnus-unread-mark))
10538
10539 (defun gnus-summary-clear-mark-backward (n)
10540   "Clear marks from N articles backward.
10541 The difference between N and the number of marks cleared is returned."
10542   (interactive "p")
10543   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10544
10545 (defun gnus-summary-mark-unread-as-read ()
10546   "Intended to be used by `gnus-summary-mark-article-hook'."
10547   (when (memq gnus-current-article gnus-newsgroup-unreads)
10548     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10549
10550 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10551   "Intended to be used by `gnus-summary-mark-article-hook'."
10552   (let ((mark (gnus-summary-article-mark)))
10553     (when (or (gnus-unread-mark-p mark)
10554               (gnus-read-mark-p mark))
10555       (gnus-summary-mark-article gnus-current-article
10556                                  (or new-mark gnus-read-mark)))))
10557
10558 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10559   "Intended to be used by `gnus-summary-mark-article-hook'."
10560   (let ((mark (gnus-summary-article-mark)))
10561     (when (or (gnus-unread-mark-p mark)
10562               (gnus-read-mark-p mark))
10563       (gnus-summary-mark-article (gnus-summary-article-number)
10564                                  (or new-mark gnus-read-mark)))))
10565
10566 (defun gnus-summary-mark-unread-as-ticked ()
10567   "Intended to be used by `gnus-summary-mark-article-hook'."
10568   (when (memq gnus-current-article gnus-newsgroup-unreads)
10569     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10570
10571 (defun gnus-summary-mark-region-as-read (point mark all)
10572   "Mark all unread articles between point and mark as read.
10573 If given a prefix, mark all articles between point and mark as read,
10574 even ticked and dormant ones."
10575   (interactive "r\nP")
10576   (save-excursion
10577     (let (article)
10578       (goto-char point)
10579       (beginning-of-line)
10580       (while (and
10581               (< (point) mark)
10582               (progn
10583                 (when (or all
10584                           (memq (setq article (gnus-summary-article-number))
10585                                 gnus-newsgroup-unreads))
10586                   (gnus-summary-mark-article article gnus-del-mark))
10587                 t)
10588               (gnus-summary-find-next))))))
10589
10590 (defun gnus-summary-mark-below (score mark)
10591   "Mark articles with score less than SCORE with MARK."
10592   (interactive "P\ncMark: ")
10593   (setq score (if score
10594                   (prefix-numeric-value score)
10595                 (or gnus-summary-default-score 0)))
10596   (save-excursion
10597     (set-buffer gnus-summary-buffer)
10598     (goto-char (point-min))
10599     (while
10600         (progn
10601           (and (< (gnus-summary-article-score) score)
10602                (gnus-summary-mark-article nil mark))
10603           (gnus-summary-find-next)))))
10604
10605 (defun gnus-summary-kill-below (&optional score)
10606   "Mark articles with score below SCORE as read."
10607   (interactive "P")
10608   (gnus-summary-mark-below score gnus-killed-mark))
10609
10610 (defun gnus-summary-clear-above (&optional score)
10611   "Clear all marks from articles with score above SCORE."
10612   (interactive "P")
10613   (gnus-summary-mark-above score gnus-unread-mark))
10614
10615 (defun gnus-summary-tick-above (&optional score)
10616   "Tick all articles with score above SCORE."
10617   (interactive "P")
10618   (gnus-summary-mark-above score gnus-ticked-mark))
10619
10620 (defun gnus-summary-mark-above (score mark)
10621   "Mark articles with score over SCORE with MARK."
10622   (interactive "P\ncMark: ")
10623   (setq score (if score
10624                   (prefix-numeric-value score)
10625                 (or gnus-summary-default-score 0)))
10626   (save-excursion
10627     (set-buffer gnus-summary-buffer)
10628     (goto-char (point-min))
10629     (while (and (progn
10630                   (when (> (gnus-summary-article-score) score)
10631                     (gnus-summary-mark-article nil mark))
10632                   t)
10633                 (gnus-summary-find-next)))))
10634
10635 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10636 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10637 (defun gnus-summary-limit-include-expunged (&optional no-error)
10638   "Display all the hidden articles that were expunged for low scores."
10639   (interactive)
10640   (let ((buffer-read-only nil))
10641     (let ((scored gnus-newsgroup-scored)
10642           headers h)
10643       (while scored
10644         (unless (gnus-summary-article-header (caar scored))
10645           (and (setq h (gnus-number-to-header (caar scored)))
10646                (< (cdar scored) gnus-summary-expunge-below)
10647                (push h headers)))
10648         (setq scored (cdr scored)))
10649       (if (not headers)
10650           (when (not no-error)
10651             (error "No expunged articles hidden"))
10652         (goto-char (point-min))
10653         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10654         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10655         (mapcar (lambda (x) (push (mail-header-number x)
10656                                   gnus-newsgroup-limit))
10657                 headers)
10658         (gnus-summary-prepare-unthreaded (nreverse headers))
10659         (goto-char (point-min))
10660         (gnus-summary-position-point)
10661         t))))
10662
10663 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10664   "Mark all unread articles in this newsgroup as read.
10665 If prefix argument ALL is non-nil, ticked and dormant articles will
10666 also be marked as read.
10667 If QUIETLY is non-nil, no questions will be asked.
10668
10669 If TO-HERE is non-nil, it should be a point in the buffer.  All
10670 articles before (after, if REVERSE is set) this point will be marked
10671 as read.
10672
10673 Note that this function will only catch up the unread article
10674 in the current summary buffer limitation.
10675
10676 The number of articles marked as read is returned."
10677   (interactive "P")
10678   (prog1
10679       (save-excursion
10680         (when (or quietly
10681                   (not gnus-interactive-catchup) ;Without confirmation?
10682                   gnus-expert-user
10683                   (gnus-y-or-n-p
10684                    (if all
10685                        "Mark absolutely all articles as read? "
10686                      "Mark all unread articles as read? ")))
10687           (if (and not-mark
10688                    (not gnus-newsgroup-adaptive)
10689                    (not gnus-newsgroup-auto-expire)
10690                    (not gnus-suppress-duplicates)
10691                    (or (not gnus-use-cache)
10692                        (eq gnus-use-cache 'passive)))
10693               (progn
10694                 (when all
10695                   (setq gnus-newsgroup-marked nil
10696                         gnus-newsgroup-spam-marked nil
10697                         gnus-newsgroup-dormant nil))
10698                 (setq gnus-newsgroup-unreads
10699                       (gnus-sorted-nunion
10700                        (gnus-intersection gnus-newsgroup-unreads
10701                                           gnus-newsgroup-downloadable)
10702                        gnus-newsgroup-unfetched)))
10703             ;; We actually mark all articles as canceled, which we
10704             ;; have to do when using auto-expiry or adaptive scoring.
10705             (gnus-summary-show-all-threads)
10706             (if (and to-here reverse)
10707                 (progn
10708                   (goto-char to-here)
10709                   (gnus-summary-mark-current-read-and-unread-as-read
10710                    gnus-catchup-mark)
10711                   (while (gnus-summary-find-next (not all))
10712                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10713               (when (gnus-summary-first-subject (not all))
10714                 (while (and
10715                         (if to-here (< (point) to-here) t)
10716                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10717                         (gnus-summary-find-next (not all))))))
10718             (gnus-set-mode-line 'summary))
10719           t))
10720     (gnus-summary-position-point)))
10721
10722 (defun gnus-summary-catchup-to-here (&optional all)
10723   "Mark all unticked articles before the current one as read.
10724 If ALL is non-nil, also mark ticked and dormant articles as read."
10725   (interactive "P")
10726   (save-excursion
10727     (gnus-save-hidden-threads
10728       (let ((beg (point)))
10729         ;; We check that there are unread articles.
10730         (when (or all (gnus-summary-find-prev))
10731           (gnus-summary-catchup all t beg)))))
10732   (gnus-summary-position-point))
10733
10734 (defun gnus-summary-catchup-from-here (&optional all)
10735   "Mark all unticked articles after (and including) the current one as read.
10736 If ALL is non-nil, also mark ticked and dormant articles as read."
10737   (interactive "P")
10738   (save-excursion
10739     (gnus-save-hidden-threads
10740       (let ((beg (point)))
10741         ;; We check that there are unread articles.
10742         (when (or all (gnus-summary-find-next))
10743           (gnus-summary-catchup all t beg nil t)))))
10744   (gnus-summary-position-point))
10745
10746 (defun gnus-summary-catchup-all (&optional quietly)
10747   "Mark all articles in this newsgroup as read.
10748 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10749 instead, which marks only unread articles as read."
10750   (interactive "P")
10751   (gnus-summary-catchup t quietly))
10752
10753 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10754   "Mark all unread articles in this group as read, then exit.
10755 If prefix argument ALL is non-nil, all articles are marked as read.
10756 If QUIETLY is non-nil, no questions will be asked."
10757   (interactive "P")
10758   (when (gnus-summary-catchup all quietly nil 'fast)
10759     ;; Select next newsgroup or exit.
10760     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10761              (eq gnus-auto-select-next 'quietly))
10762         (gnus-summary-next-group nil)
10763       (gnus-summary-exit))))
10764
10765 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10766   "Mark all articles in this newsgroup as read, and then exit.
10767 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10768 instead, which marks only unread articles as read."
10769   (interactive "P")
10770   (gnus-summary-catchup-and-exit t quietly))
10771
10772 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10773   "Mark all articles in this group as read and select the next group.
10774 If given a prefix, mark all articles, unread as well as ticked, as
10775 read."
10776   (interactive "P")
10777   (save-excursion
10778     (gnus-summary-catchup all))
10779   (gnus-summary-next-group))
10780
10781 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
10782   "Mark all articles in this group as read and select the previous group.
10783 If given a prefix, mark all articles, unread as well as ticked, as
10784 read."
10785   (interactive "P")
10786   (save-excursion
10787     (gnus-summary-catchup all))
10788   (gnus-summary-next-group nil nil t))
10789
10790 ;;;
10791 ;;; with article
10792 ;;;
10793
10794 (defmacro gnus-with-article (article &rest forms)
10795   "Select ARTICLE and perform FORMS in the original article buffer.
10796 Then replace the article with the result."
10797   `(progn
10798      ;; We don't want the article to be marked as read.
10799      (let (gnus-mark-article-hook)
10800        (gnus-summary-select-article t t nil ,article))
10801      (set-buffer gnus-original-article-buffer)
10802      ,@forms
10803      (if (not (gnus-check-backend-function
10804                'request-replace-article (car gnus-article-current)))
10805          (gnus-message 5 "Read-only group; not replacing")
10806        (unless (gnus-request-replace-article
10807                 ,article (car gnus-article-current)
10808                 (current-buffer) t)
10809          (error "Couldn't replace article")))
10810      ;; The cache and backlog have to be flushed somewhat.
10811      (when gnus-keep-backlog
10812        (gnus-backlog-remove-article
10813         (car gnus-article-current) (cdr gnus-article-current)))
10814      (when gnus-use-cache
10815        (gnus-cache-update-article
10816         (car gnus-article-current) (cdr gnus-article-current)))))
10817
10818 (put 'gnus-with-article 'lisp-indent-function 1)
10819 (put 'gnus-with-article 'edebug-form-spec '(form body))
10820
10821 ;; Thread-based commands.
10822
10823 (defun gnus-summary-articles-in-thread (&optional article)
10824   "Return a list of all articles in the current thread.
10825 If ARTICLE is non-nil, return all articles in the thread that starts
10826 with that article."
10827   (let* ((article (or article (gnus-summary-article-number)))
10828          (data (gnus-data-find-list article))
10829          (top-level (gnus-data-level (car data)))
10830          (top-subject
10831           (cond ((null gnus-thread-operation-ignore-subject)
10832                  (gnus-simplify-subject-re
10833                   (mail-header-subject (gnus-data-header (car data)))))
10834                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10835                  (gnus-simplify-subject-fuzzy
10836                   (mail-header-subject (gnus-data-header (car data)))))
10837                 (t nil)))
10838          (end-point (save-excursion
10839                       (if (gnus-summary-go-to-next-thread)
10840                           (point) (point-max))))
10841          articles)
10842     (while (and data
10843                 (< (gnus-data-pos (car data)) end-point))
10844       (when (or (not top-subject)
10845                 (string= top-subject
10846                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10847                              (gnus-simplify-subject-fuzzy
10848                               (mail-header-subject
10849                                (gnus-data-header (car data))))
10850                            (gnus-simplify-subject-re
10851                             (mail-header-subject
10852                              (gnus-data-header (car data)))))))
10853         (push (gnus-data-number (car data)) articles))
10854       (unless (and (setq data (cdr data))
10855                    (> (gnus-data-level (car data)) top-level))
10856         (setq data nil)))
10857     ;; Return the list of articles.
10858     (nreverse articles)))
10859
10860 (defun gnus-summary-rethread-current ()
10861   "Rethread the thread the current article is part of."
10862   (interactive)
10863   (let* ((gnus-show-threads t)
10864          (article (gnus-summary-article-number))
10865          (id (mail-header-id (gnus-summary-article-header)))
10866          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10867     (unless id
10868       (error "No article on the current line"))
10869     (gnus-rebuild-thread id)
10870     (gnus-summary-goto-subject article)))
10871
10872 (defun gnus-summary-reparent-thread ()
10873   "Make the current article child of the marked (or previous) article.
10874
10875 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10876 is non-nil or the Subject: of both articles are the same."
10877   (interactive)
10878   (unless (not (gnus-group-read-only-p))
10879     (error "The current newsgroup does not support article editing"))
10880   (unless (<= (length gnus-newsgroup-processable) 1)
10881     (error "No more than one article may be marked"))
10882   (let ((child (gnus-summary-article-number))
10883         ;; First grab the marked article, otherwise one line up.
10884         (parent (if (not (null gnus-newsgroup-processable))
10885                     (car gnus-newsgroup-processable)
10886                   (save-excursion
10887                     (if (eq (forward-line -1) 0)
10888                         (gnus-summary-article-number)
10889                       (error "Beginning of summary buffer"))))))
10890     (gnus-summary-reparent-children parent (list child))))
10891
10892 (defun gnus-summary-reparent-children (parent children)
10893   "Make PARENT the parent of CHILDREN.
10894 When called interactively, PARENT is is current article and
10895 CHILDREN are the process-marked articles."
10896   (interactive
10897    (list (gnus-summary-article-number)
10898          (gnus-summary-work-articles nil)))
10899   (dolist (child children)
10900     (save-window-excursion
10901       (let ((gnus-article-buffer " *reparent*"))
10902         (unless (not (eq parent child))
10903           (error "An article may not be self-referential"))
10904         (let ((message-id (mail-header-id
10905                            (gnus-summary-article-header parent))))
10906           (unless (and message-id (not (equal message-id "")))
10907             (error "No message-id in desired parent"))
10908           (gnus-with-article child
10909             (save-restriction
10910               (goto-char (point-min))
10911               (message-narrow-to-head)
10912               (if (re-search-forward "^References: " nil t)
10913                   (progn
10914                     (re-search-forward "^[^ \t]" nil t)
10915                     (forward-line -1)
10916                     (end-of-line)
10917                     (insert " " message-id))
10918                 (insert "References: " message-id "\n"))))
10919           (set-buffer gnus-summary-buffer)
10920           (gnus-summary-unmark-all-processable)
10921           (gnus-summary-update-article child)
10922           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10923             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10924           (gnus-summary-rethread-current)
10925           (gnus-message 3 "Article %d is now the child of article %d"
10926                         child parent))))))
10927
10928 (defun gnus-summary-toggle-threads (&optional arg)
10929   "Toggle showing conversation threads.
10930 If ARG is positive number, turn showing conversation threads on."
10931   (interactive "P")
10932   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10933     (setq gnus-show-threads
10934           (if (null arg) (not gnus-show-threads)
10935             (> (prefix-numeric-value arg) 0)))
10936     (gnus-summary-prepare)
10937     (gnus-summary-goto-subject current)
10938     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10939     (gnus-summary-position-point)))
10940
10941 (defun gnus-summary-show-all-threads ()
10942   "Show all threads."
10943   (interactive)
10944   (save-excursion
10945     (let ((buffer-read-only nil))
10946       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10947   (gnus-summary-position-point))
10948
10949 (defun gnus-summary-show-thread ()
10950   "Show thread subtrees.
10951 Returns nil if no thread was there to be shown."
10952   (interactive)
10953   (let ((buffer-read-only nil)
10954         (orig (point))
10955         (end (point-at-eol))
10956         ;; Leave point at bol
10957         (beg (progn (beginning-of-line) (point))))
10958     (prog1
10959         ;; Any hidden lines here?
10960         (search-forward "\r" end t)
10961       (subst-char-in-region beg end ?\^M ?\n t)
10962       (goto-char orig)
10963       (gnus-summary-position-point))))
10964
10965 (defun gnus-summary-maybe-hide-threads ()
10966   "If requested, hide the threads that should be hidden."
10967   (when (and gnus-show-threads
10968              gnus-thread-hide-subtree)
10969     (gnus-summary-hide-all-threads
10970      (if (or (consp gnus-thread-hide-subtree)
10971              (functionp gnus-thread-hide-subtree))
10972          (gnus-make-predicate gnus-thread-hide-subtree)
10973        nil))))
10974
10975 ;;; Hiding predicates.
10976
10977 (defun gnus-article-unread-p (header)
10978   (memq (mail-header-number header) gnus-newsgroup-unreads))
10979
10980 (defun gnus-article-unseen-p (header)
10981   (memq (mail-header-number header) gnus-newsgroup-unseen))
10982
10983 (defun gnus-map-articles (predicate articles)
10984   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10985   (apply 'gnus-or (mapcar predicate
10986                           (mapcar (lambda (number)
10987                                     (gnus-summary-article-header number))
10988                                   articles))))
10989
10990 (defun gnus-summary-hide-all-threads (&optional predicate)
10991   "Hide all thread subtrees.
10992 If PREDICATE is supplied, threads that satisfy this predicate
10993 will not be hidden."
10994   (interactive)
10995   (save-excursion
10996     (goto-char (point-min))
10997     (let ((end nil))
10998       (while (not end)
10999         (when (or (not predicate)
11000                   (gnus-map-articles
11001                    predicate (gnus-summary-article-children)))
11002             (gnus-summary-hide-thread))
11003         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11004   (gnus-summary-position-point))
11005
11006 (defun gnus-summary-hide-thread ()
11007   "Hide thread subtrees.
11008 If PREDICATE is supplied, threads that satisfy this predicate
11009 will not be hidden.
11010 Returns nil if no threads were there to be hidden."
11011   (interactive)
11012   (let ((buffer-read-only nil)
11013         (start (point))
11014         (article (gnus-summary-article-number)))
11015     (goto-char start)
11016     ;; Go forward until either the buffer ends or the subthread
11017     ;; ends.
11018     (when (and (not (eobp))
11019                (or (zerop (gnus-summary-next-thread 1 t))
11020                    (goto-char (point-max))))
11021       (prog1
11022           (if (and (> (point) start)
11023                    (search-backward "\n" start t))
11024               (progn
11025                 (subst-char-in-region start (point) ?\n ?\^M)
11026                 (gnus-summary-goto-subject article))
11027             (goto-char start)
11028             nil)))))
11029
11030 (defun gnus-summary-go-to-next-thread (&optional previous)
11031   "Go to the same level (or less) next thread.
11032 If PREVIOUS is non-nil, go to previous thread instead.
11033 Return the article number moved to, or nil if moving was impossible."
11034   (let ((level (gnus-summary-thread-level))
11035         (way (if previous -1 1))
11036         (beg (point)))
11037     (forward-line way)
11038     (while (and (not (eobp))
11039                 (< level (gnus-summary-thread-level)))
11040       (forward-line way))
11041     (if (eobp)
11042         (progn
11043           (goto-char beg)
11044           nil)
11045       (setq beg (point))
11046       (prog1
11047           (gnus-summary-article-number)
11048         (goto-char beg)))))
11049
11050 (defun gnus-summary-next-thread (n &optional silent)
11051   "Go to the same level next N'th thread.
11052 If N is negative, search backward instead.
11053 Returns the difference between N and the number of skips actually
11054 done.
11055
11056 If SILENT, don't output messages."
11057   (interactive "p")
11058   (let ((backward (< n 0))
11059         (n (abs n)))
11060     (while (and (> n 0)
11061                 (gnus-summary-go-to-next-thread backward))
11062       (decf n))
11063     (unless silent
11064       (gnus-summary-position-point))
11065     (when (and (not silent) (/= 0 n))
11066       (gnus-message 7 "No more threads"))
11067     n))
11068
11069 (defun gnus-summary-prev-thread (n)
11070   "Go to the same level previous N'th thread.
11071 Returns the difference between N and the number of skips actually
11072 done."
11073   (interactive "p")
11074   (gnus-summary-next-thread (- n)))
11075
11076 (defun gnus-summary-go-down-thread ()
11077   "Go down one level in the current thread."
11078   (let ((children (gnus-summary-article-children)))
11079     (when children
11080       (gnus-summary-goto-subject (car children)))))
11081
11082 (defun gnus-summary-go-up-thread ()
11083   "Go up one level in the current thread."
11084   (let ((parent (gnus-summary-article-parent)))
11085     (when parent
11086       (gnus-summary-goto-subject parent))))
11087
11088 (defun gnus-summary-down-thread (n)
11089   "Go down thread N steps.
11090 If N is negative, go up instead.
11091 Returns the difference between N and how many steps down that were
11092 taken."
11093   (interactive "p")
11094   (let ((up (< n 0))
11095         (n (abs n)))
11096     (while (and (> n 0)
11097                 (if up (gnus-summary-go-up-thread)
11098                   (gnus-summary-go-down-thread)))
11099       (setq n (1- n)))
11100     (gnus-summary-position-point)
11101     (when (/= 0 n)
11102       (gnus-message 7 "Can't go further"))
11103     n))
11104
11105 (defun gnus-summary-up-thread (n)
11106   "Go up thread N steps.
11107 If N is negative, go down instead.
11108 Returns the difference between N and how many steps down that were
11109 taken."
11110   (interactive "p")
11111   (gnus-summary-down-thread (- n)))
11112
11113 (defun gnus-summary-top-thread ()
11114   "Go to the top of the thread."
11115   (interactive)
11116   (while (gnus-summary-go-up-thread))
11117   (gnus-summary-article-number))
11118
11119 (defun gnus-summary-kill-thread (&optional unmark)
11120   "Mark articles under current thread as read.
11121 If the prefix argument is positive, remove any kinds of marks.
11122 If the prefix argument is negative, tick articles instead."
11123   (interactive "P")
11124   (when unmark
11125     (setq unmark (prefix-numeric-value unmark)))
11126   (let ((articles (gnus-summary-articles-in-thread)))
11127     (save-excursion
11128       ;; Expand the thread.
11129       (gnus-summary-show-thread)
11130       ;; Mark all the articles.
11131       (while articles
11132         (gnus-summary-goto-subject (car articles))
11133         (cond ((null unmark)
11134                (gnus-summary-mark-article-as-read gnus-killed-mark))
11135               ((> unmark 0)
11136                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11137               (t
11138                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11139         (setq articles (cdr articles))))
11140     ;; Hide killed subtrees.
11141     (and (null unmark)
11142          gnus-thread-hide-killed
11143          (gnus-summary-hide-thread))
11144     ;; If marked as read, go to next unread subject.
11145     (when (null unmark)
11146       ;; Go to next unread subject.
11147       (gnus-summary-next-subject 1 t)))
11148   (gnus-set-mode-line 'summary))
11149
11150 ;; Summary sorting commands
11151
11152 (defun gnus-summary-sort-by-number (&optional reverse)
11153   "Sort the summary buffer by article number.
11154 Argument REVERSE means reverse order."
11155   (interactive "P")
11156   (gnus-summary-sort 'number reverse))
11157
11158 (defun gnus-summary-sort-by-random (&optional reverse)
11159   "Randomize the order in the summary buffer.
11160 Argument REVERSE means to randomize in reverse order."
11161   (interactive "P")
11162   (gnus-summary-sort 'random reverse))
11163
11164 (defun gnus-summary-sort-by-author (&optional reverse)
11165   "Sort the summary buffer by author name alphabetically.
11166 If `case-fold-search' is non-nil, case of letters is ignored.
11167 Argument REVERSE means reverse order."
11168   (interactive "P")
11169   (gnus-summary-sort 'author reverse))
11170
11171 (defun gnus-summary-sort-by-recipient (&optional reverse)
11172   "Sort the summary buffer by recipient name alphabetically.
11173 If `case-fold-search' is non-nil, case of letters is ignored.
11174 Argument REVERSE means reverse order."
11175   (interactive "P")
11176   (gnus-summary-sort 'recipient reverse))
11177
11178 (defun gnus-summary-sort-by-subject (&optional reverse)
11179   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11180 If `case-fold-search' is non-nil, case of letters is ignored.
11181 Argument REVERSE means reverse order."
11182   (interactive "P")
11183   (gnus-summary-sort 'subject reverse))
11184
11185 (defun gnus-summary-sort-by-date (&optional reverse)
11186   "Sort the summary buffer by date.
11187 Argument REVERSE means reverse order."
11188   (interactive "P")
11189   (gnus-summary-sort 'date reverse))
11190
11191 (defun gnus-summary-sort-by-score (&optional reverse)
11192   "Sort the summary buffer by score.
11193 Argument REVERSE means reverse order."
11194   (interactive "P")
11195   (gnus-summary-sort 'score reverse))
11196
11197 (defun gnus-summary-sort-by-lines (&optional reverse)
11198   "Sort the summary buffer by the number of lines.
11199 Argument REVERSE means reverse order."
11200   (interactive "P")
11201   (gnus-summary-sort 'lines reverse))
11202
11203 (defun gnus-summary-sort-by-chars (&optional reverse)
11204   "Sort the summary buffer by article length.
11205 Argument REVERSE means reverse order."
11206   (interactive "P")
11207   (gnus-summary-sort 'chars reverse))
11208
11209 (defun gnus-summary-sort-by-original (&optional reverse)
11210   "Sort the summary buffer using the default sorting method.
11211 Argument REVERSE means reverse order."
11212   (interactive "P")
11213   (let* ((buffer-read-only)
11214          (gnus-summary-prepare-hook nil))
11215     ;; We do the sorting by regenerating the threads.
11216     (gnus-summary-prepare)
11217     ;; Hide subthreads if needed.
11218     (gnus-summary-maybe-hide-threads)))
11219
11220 (defun gnus-summary-sort (predicate reverse)
11221   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11222   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11223          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11224          (gnus-thread-sort-functions
11225           (if (not reverse)
11226               thread
11227             `(lambda (t1 t2)
11228                (,thread t2 t1))))
11229          (gnus-sort-gathered-threads-function
11230           gnus-thread-sort-functions)
11231          (gnus-article-sort-functions
11232           (if (not reverse)
11233               article
11234             `(lambda (t1 t2)
11235                (,article t2 t1))))
11236          (buffer-read-only)
11237          (gnus-summary-prepare-hook nil))
11238     ;; We do the sorting by regenerating the threads.
11239     (gnus-summary-prepare)
11240     ;; Hide subthreads if needed.
11241     (gnus-summary-maybe-hide-threads)))
11242
11243 ;; Summary saving commands.
11244
11245 (defun gnus-summary-save-article (&optional n not-saved)
11246   "Save the current article using the default saver function.
11247 If N is a positive number, save the N next articles.
11248 If N is a negative number, save the N previous articles.
11249 If N is nil and any articles have been marked with the process mark,
11250 save those articles instead.
11251 The variable `gnus-default-article-saver' specifies the saver function.
11252
11253 If the optional second argument NOT-SAVED is non-nil, articles saved
11254 will not be marked as saved."
11255   (interactive "P")
11256   (require 'gnus-art)
11257   (let* ((articles (gnus-summary-work-articles n))
11258          (save-buffer (save-excursion
11259                         (nnheader-set-temp-buffer " *Gnus Save*")))
11260          (num (length articles))
11261          ;; Whether to save decoded articles or raw articles.
11262          (decode (when gnus-article-save-coding-system
11263                    (get gnus-default-article-saver :decode)))
11264          ;; When saving many articles in a single file, use the other
11265          ;; function to save articles other than the first one.
11266          (saver2 (get gnus-default-article-saver :function))
11267          (gnus-prompt-before-saving (if saver2
11268                                         t
11269                                       gnus-prompt-before-saving))
11270          (gnus-default-article-saver gnus-default-article-saver)
11271          header file)
11272     (dolist (article articles)
11273       (setq header (gnus-summary-article-header article))
11274       (if (not (vectorp header))
11275           ;; This is a pseudo-article.
11276           (if (assq 'name header)
11277               (gnus-copy-file (cdr (assq 'name header)))
11278             (gnus-message 1 "Article %d is unsaveable" article))
11279         ;; This is a real article.
11280         (save-window-excursion
11281           (let ((gnus-display-mime-function (when decode
11282                                               gnus-display-mime-function))
11283                 (gnus-article-prepare-hook (when decode
11284                                              gnus-article-prepare-hook)))
11285             (gnus-summary-select-article t nil nil article)
11286             (gnus-summary-goto-subject article)))
11287         (save-excursion
11288           (set-buffer save-buffer)
11289           (erase-buffer)
11290           (insert-buffer-substring (if decode
11291                                        gnus-article-buffer
11292                                      gnus-original-article-buffer)))
11293         (setq file (gnus-article-save save-buffer file num))
11294         (gnus-summary-remove-process-mark article)
11295         (unless not-saved
11296           (gnus-summary-set-saved-mark article)))
11297       (when saver2
11298         (setq gnus-default-article-saver saver2
11299               saver2 nil)))
11300     (gnus-kill-buffer save-buffer)
11301     (gnus-summary-position-point)
11302     (gnus-set-mode-line 'summary)
11303     n))
11304
11305 (defun gnus-summary-pipe-output (&optional arg headers)
11306   "Pipe the current article to a subprocess.
11307 If N is a positive number, pipe the N next articles.
11308 If N is a negative number, pipe the N previous articles.
11309 If N is nil and any articles have been marked with the process mark,
11310 pipe those articles instead.
11311 If HEADERS (the symbolic prefix), include the headers, too."
11312   (interactive (gnus-interactive "P\ny"))
11313   (require 'gnus-art)
11314   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
11315         (gnus-save-all-headers (or headers gnus-save-all-headers)))
11316     (gnus-summary-save-article arg t))
11317   (let ((buffer (get-buffer "*Shell Command Output*")))
11318     (when (and buffer
11319                (not (zerop (buffer-size buffer))))
11320       (gnus-configure-windows 'pipe))))
11321
11322 (defun gnus-summary-save-article-mail (&optional arg)
11323   "Append the current article to a Unix mail box file.
11324 If N is a positive number, save the N next articles.
11325 If N is a negative number, save the N previous articles.
11326 If N is nil and any articles have been marked with the process mark,
11327 save those articles instead."
11328   (interactive "P")
11329   (require 'gnus-art)
11330   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11331     (gnus-summary-save-article arg)))
11332
11333 (defun gnus-summary-save-article-rmail (&optional arg)
11334   "Append the current article to an rmail file.
11335 If N is a positive number, save the N next articles.
11336 If N is a negative number, save the N previous articles.
11337 If N is nil and any articles have been marked with the process mark,
11338 save those articles instead."
11339   (interactive "P")
11340   (require 'gnus-art)
11341   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11342     (gnus-summary-save-article arg)))
11343
11344 (defun gnus-summary-save-article-file (&optional arg)
11345   "Append the current article to a file.
11346 If N is a positive number, save the N next articles.
11347 If N is a negative number, save the N previous articles.
11348 If N is nil and any articles have been marked with the process mark,
11349 save those articles instead."
11350   (interactive "P")
11351   (require 'gnus-art)
11352   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11353     (gnus-summary-save-article arg)))
11354
11355 (defun gnus-summary-write-article-file (&optional arg)
11356   "Write the current article to a file, deleting the previous file.
11357 If N is a positive number, save the N next articles.
11358 If N is a negative number, save the N previous articles.
11359 If N is nil and any articles have been marked with the process mark,
11360 save those articles instead."
11361   (interactive "P")
11362   (require 'gnus-art)
11363   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11364     (gnus-summary-save-article arg)))
11365
11366 (defun gnus-summary-save-article-body-file (&optional arg)
11367   "Append the current article body to a file.
11368 If N is a positive number, save the N next articles.
11369 If N is a negative number, save the N previous articles.
11370 If N is nil and any articles have been marked with the process mark,
11371 save those articles instead."
11372   (interactive "P")
11373   (require 'gnus-art)
11374   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11375     (gnus-summary-save-article arg)))
11376
11377 (defun gnus-summary-write-article-body-file (&optional arg)
11378   "Write the current article body to a file, deleting the previous file.
11379 If N is a positive number, save the N next articles.
11380 If N is a negative number, save the N previous articles.
11381 If N is nil and any articles have been marked with the process mark,
11382 save those articles instead."
11383   (interactive "P")
11384   (require 'gnus-art)
11385   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11386     (gnus-summary-save-article arg)))
11387
11388 (defun gnus-summary-muttprint (&optional arg)
11389   "Print the current article using Muttprint.
11390 If N is a positive number, save the N next articles.
11391 If N is a negative number, save the N previous articles.
11392 If N is nil and any articles have been marked with the process mark,
11393 save those articles instead."
11394   (interactive "P")
11395   (require 'gnus-art)
11396   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11397     (gnus-summary-save-article arg t)))
11398
11399 (defun gnus-summary-pipe-message (program)
11400   "Pipe the current article through PROGRAM."
11401   (interactive "sProgram: ")
11402   (gnus-summary-select-article)
11403   (let ((mail-header-separator ""))
11404     (gnus-eval-in-buffer-window gnus-article-buffer
11405       (save-restriction
11406         (widen)
11407         (let ((start (window-start))
11408               buffer-read-only)
11409           (message-pipe-buffer-body program)
11410           (set-window-start (get-buffer-window (current-buffer)) start))))))
11411
11412 (defun gnus-get-split-value (methods)
11413   "Return a value based on the split METHODS."
11414   (let (split-name method result match)
11415     (when methods
11416       (save-excursion
11417         (set-buffer gnus-original-article-buffer)
11418         (save-restriction
11419           (nnheader-narrow-to-headers)
11420           (while (and methods (not split-name))
11421             (goto-char (point-min))
11422             (setq method (pop methods))
11423             (setq match (car method))
11424             (when (cond
11425                    ((stringp match)
11426                     ;; Regular expression.
11427                     (ignore-errors
11428                       (re-search-forward match nil t)))
11429                    ((functionp match)
11430                     ;; Function.
11431                     (save-restriction
11432                       (widen)
11433                       (setq result (funcall match gnus-newsgroup-name))))
11434                    ((consp match)
11435                     ;; Form.
11436                     (save-restriction
11437                       (widen)
11438                       (setq result (eval match)))))
11439               (setq split-name (cdr method))
11440               (cond ((stringp result)
11441                      (push (expand-file-name
11442                             result gnus-article-save-directory)
11443                            split-name))
11444                     ((consp result)
11445                      (setq split-name (append result split-name)))))))))
11446     (nreverse split-name)))
11447
11448 (defun gnus-valid-move-group-p (group)
11449   (and (boundp group)
11450        (symbol-name group)
11451        (symbol-value group)
11452        (gnus-get-function (gnus-find-method-for-group
11453                            (symbol-name group)) 'request-accept-article t)))
11454
11455 (defun gnus-read-move-group-name (prompt default articles prefix)
11456   "Read a group name."
11457   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11458          (minibuffer-confirm-incomplete nil) ; XEmacs
11459          (prom
11460           (format "%s %s to"
11461                   prompt
11462                   (if (> (length articles) 1)
11463                       (format "these %d articles" (length articles))
11464                     "this article")))
11465          (to-newsgroup
11466           (cond
11467            ((null split-name)
11468             (gnus-completing-read-with-default
11469              default prom
11470              gnus-active-hashtb
11471              'gnus-valid-move-group-p
11472              nil prefix
11473              'gnus-group-history))
11474            ((= 1 (length split-name))
11475             (gnus-completing-read-with-default
11476              (car split-name) prom
11477              gnus-active-hashtb
11478              'gnus-valid-move-group-p
11479              nil nil
11480              'gnus-group-history))
11481            (t
11482             (gnus-completing-read-with-default
11483              nil prom
11484              (mapcar 'list (nreverse split-name))
11485              nil nil nil
11486              'gnus-group-history))))
11487          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11488     (when to-newsgroup
11489       (if (or (string= to-newsgroup "")
11490               (string= to-newsgroup prefix))
11491           (setq to-newsgroup default))
11492       (unless to-newsgroup
11493         (error "No group name entered"))
11494       (or (gnus-active to-newsgroup)
11495           (gnus-activate-group to-newsgroup nil nil to-method)
11496           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11497                                      to-newsgroup))
11498               (or (and (gnus-request-create-group to-newsgroup to-method)
11499                        (gnus-activate-group
11500                         to-newsgroup nil nil to-method)
11501                        (gnus-subscribe-group to-newsgroup))
11502                   (error "Couldn't create group %s" to-newsgroup)))
11503           (error "No such group: %s" to-newsgroup)))
11504     to-newsgroup))
11505
11506 (defvar gnus-summary-save-parts-counter)
11507
11508 (defun gnus-summary-save-parts (type dir n &optional reverse)
11509   "Save parts matching TYPE to DIR.
11510 If REVERSE, save parts that do not match TYPE."
11511   (interactive
11512    (list (read-string "Save parts of type: "
11513                       (or (car gnus-summary-save-parts-type-history)
11514                           gnus-summary-save-parts-default-mime)
11515                       'gnus-summary-save-parts-type-history)
11516          (setq gnus-summary-save-parts-last-directory
11517                (read-file-name "Save to directory: "
11518                                gnus-summary-save-parts-last-directory
11519                                nil t))
11520          current-prefix-arg))
11521   (gnus-summary-iterate n
11522     (let ((gnus-display-mime-function nil)
11523           (gnus-inhibit-treatment t))
11524       (gnus-summary-select-article))
11525     (save-excursion
11526       (set-buffer gnus-article-buffer)
11527       (let ((handles (or gnus-article-mime-handles
11528                          (mm-dissect-buffer nil gnus-article-loose-mime)
11529                          (and gnus-article-emulate-mime
11530                               (mm-uu-dissect))))
11531             (gnus-summary-save-parts-counter 1))
11532         (when handles
11533           (gnus-summary-save-parts-1 type dir handles reverse)
11534           (unless gnus-article-mime-handles ;; Don't destroy this case.
11535             (mm-destroy-parts handles)))))))
11536
11537 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11538   (if (stringp (car handle))
11539       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11540               (cdr handle))
11541     (when (if reverse
11542               (not (string-match type (mm-handle-media-type handle)))
11543             (string-match type (mm-handle-media-type handle)))
11544       (let ((file (expand-file-name
11545                    (gnus-map-function
11546                     mm-file-name-rewrite-functions
11547                     (file-name-nondirectory
11548                      (or
11549                       (mail-content-type-get
11550                        (mm-handle-disposition handle) 'filename)
11551                       (mail-content-type-get
11552                        (mm-handle-type handle) 'name)
11553                       (format "%s.%d.%d" gnus-newsgroup-name
11554                               (cdr gnus-article-current)
11555                               gnus-summary-save-parts-counter))))
11556                    dir)))
11557         (incf gnus-summary-save-parts-counter)
11558         (unless (file-exists-p file)
11559           (mm-save-part-to-file handle file))))))
11560
11561 ;; Summary extract commands
11562
11563 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11564   (let ((buffer-read-only nil)
11565         (article (gnus-summary-article-number))
11566         after-article b e)
11567     (unless (gnus-summary-goto-subject article)
11568       (error "No such article: %d" article))
11569     (gnus-summary-position-point)
11570     ;; If all commands are to be bunched up on one line, we collect
11571     ;; them here.
11572     (unless gnus-view-pseudos-separately
11573       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11574             files action)
11575         (while ps
11576           (setq action (cdr (assq 'action (car ps))))
11577           (setq files (list (cdr (assq 'name (car ps)))))
11578           (while (and ps (cdr ps)
11579                       (string= (or action "1")
11580                                (or (cdr (assq 'action (cadr ps))) "2")))
11581             (push (cdr (assq 'name (cadr ps))) files)
11582             (setcdr ps (cddr ps)))
11583           (when files
11584             (when (not (string-match "%s" action))
11585               (push " " files))
11586             (push " " files)
11587             (when (assq 'execute (car ps))
11588               (setcdr (assq 'execute (car ps))
11589                       (funcall (if (string-match "%s" action)
11590                                    'format 'concat)
11591                                action
11592                                (mapconcat
11593                                 (lambda (f)
11594                                   (if (equal f " ")
11595                                       f
11596                                     (shell-quote-argument f)))
11597                                 files " ")))))
11598           (setq ps (cdr ps)))))
11599     (if (and gnus-view-pseudos (not not-view))
11600         (while pslist
11601           (when (assq 'execute (car pslist))
11602             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11603                                   (eq gnus-view-pseudos 'not-confirm)))
11604           (setq pslist (cdr pslist)))
11605       (save-excursion
11606         (while pslist
11607           (setq after-article (or (cdr (assq 'article (car pslist)))
11608                                   (gnus-summary-article-number)))
11609           (gnus-summary-goto-subject after-article)
11610           (forward-line 1)
11611           (setq b (point))
11612           (insert "    " (file-name-nondirectory
11613                           (cdr (assq 'name (car pslist))))
11614                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11615           (setq e (point))
11616           (forward-line -1)             ; back to `b'
11617           (gnus-add-text-properties
11618            b (1- e) (list 'gnus-number gnus-reffed-article-number
11619                           gnus-mouse-face-prop gnus-mouse-face))
11620           (gnus-data-enter
11621            after-article gnus-reffed-article-number
11622            gnus-unread-mark b (car pslist) 0 (- e b))
11623           (setq gnus-newsgroup-unreads
11624                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11625                                          gnus-reffed-article-number))
11626           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11627           (setq pslist (cdr pslist)))))))
11628
11629 (defun gnus-pseudos< (p1 p2)
11630   (let ((c1 (cdr (assq 'action p1)))
11631         (c2 (cdr (assq 'action p2))))
11632     (and c1 c2 (string< c1 c2))))
11633
11634 (defun gnus-request-pseudo-article (props)
11635   (cond ((assq 'execute props)
11636          (gnus-execute-command (cdr (assq 'execute props)))))
11637   (let ((gnus-current-article (gnus-summary-article-number)))
11638     (gnus-run-hooks 'gnus-mark-article-hook)))
11639
11640 (defun gnus-execute-command (command &optional automatic)
11641   (save-excursion
11642     (gnus-article-setup-buffer)
11643     (set-buffer gnus-article-buffer)
11644     (setq buffer-read-only nil)
11645     (let ((command (if automatic command
11646                      (read-string "Command: " (cons command 0)))))
11647       (erase-buffer)
11648       (insert "$ " command "\n\n")
11649       (if gnus-view-pseudo-asynchronously
11650           (start-process "gnus-execute" (current-buffer) shell-file-name
11651                          shell-command-switch command)
11652         (call-process shell-file-name nil t nil
11653                       shell-command-switch command)))))
11654
11655 ;; Summary kill commands.
11656
11657 (defun gnus-summary-edit-global-kill (article)
11658   "Edit the \"global\" kill file."
11659   (interactive (list (gnus-summary-article-number)))
11660   (gnus-group-edit-global-kill article))
11661
11662 (defun gnus-summary-edit-local-kill ()
11663   "Edit a local kill file applied to the current newsgroup."
11664   (interactive)
11665   (setq gnus-current-headers (gnus-summary-article-header))
11666   (gnus-group-edit-local-kill
11667    (gnus-summary-article-number) gnus-newsgroup-name))
11668
11669 ;;; Header reading.
11670
11671 (defun gnus-read-header (id &optional header)
11672   "Read the headers of article ID and enter them into the Gnus system."
11673   (let ((group gnus-newsgroup-name)
11674         (gnus-override-method
11675          (or
11676           gnus-override-method
11677           (and (gnus-news-group-p gnus-newsgroup-name)
11678                (car (gnus-refer-article-methods)))))
11679         where)
11680     ;; First we check to see whether the header in question is already
11681     ;; fetched.
11682     (if (stringp id)
11683         ;; This is a Message-ID.
11684         (setq header (or header (gnus-id-to-header id)))
11685       ;; This is an article number.
11686       (setq header (or header (gnus-summary-article-header id))))
11687     (if (and header
11688              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11689         ;; We have found the header.
11690         header
11691       ;; We have to really fetch the header to this article.
11692       (save-excursion
11693         (set-buffer nntp-server-buffer)
11694         (when (setq where (gnus-request-head id group))
11695           (nnheader-fold-continuation-lines)
11696           (goto-char (point-max))
11697           (insert ".\n")
11698           (goto-char (point-min))
11699           (insert "211 ")
11700           (princ (cond
11701                   ((numberp id) id)
11702                   ((cdr where) (cdr where))
11703                   (header (mail-header-number header))
11704                   (t gnus-reffed-article-number))
11705                  (current-buffer))
11706           (insert " Article retrieved.\n"))
11707         (if (or (not where)
11708                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11709             ()                          ; Malformed head.
11710           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11711             (when (and (stringp id)
11712                        (or
11713                         (not (string= (gnus-group-real-name group)
11714                                       (car where)))
11715                         (not (gnus-server-equal gnus-override-method
11716                                                 (gnus-group-method group)))))
11717               ;; If we fetched by Message-ID and the article came from
11718               ;; a different group (or server), we fudge some bogus
11719               ;; article numbers for this article.
11720               (mail-header-set-number header gnus-reffed-article-number))
11721             (save-excursion
11722               (set-buffer gnus-summary-buffer)
11723               (decf gnus-reffed-article-number)
11724               (gnus-remove-header (mail-header-number header))
11725               (push header gnus-newsgroup-headers)
11726               (setq gnus-current-headers header)
11727               (push (mail-header-number header) gnus-newsgroup-limit)))
11728           header)))))
11729
11730 (defun gnus-remove-header (number)
11731   "Remove header NUMBER from `gnus-newsgroup-headers'."
11732   (if (and gnus-newsgroup-headers
11733            (= number (mail-header-number (car gnus-newsgroup-headers))))
11734       (pop gnus-newsgroup-headers)
11735     (let ((headers gnus-newsgroup-headers))
11736       (while (and (cdr headers)
11737                   (not (= number (mail-header-number (cadr headers)))))
11738         (pop headers))
11739       (when (cdr headers)
11740         (setcdr headers (cddr headers))))))
11741
11742 ;;;
11743 ;;; summary highlights
11744 ;;;
11745
11746 (defun gnus-highlight-selected-summary ()
11747   "Highlight selected article in summary buffer."
11748   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11749   (when gnus-summary-selected-face
11750     (save-excursion
11751       (let* ((beg (point-at-bol))
11752              (end (point-at-eol))
11753              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11754              (from (if (get-text-property beg gnus-mouse-face-prop)
11755                        beg
11756                      (or (next-single-property-change
11757                           beg gnus-mouse-face-prop nil end)
11758                          beg)))
11759              (to
11760               (if (= from end)
11761                   (- from 2)
11762                 (or (next-single-property-change
11763                      from gnus-mouse-face-prop nil end)
11764                     end))))
11765         ;; If no mouse-face prop on line we will have to = from = end,
11766         ;; so we highlight the entire line instead.
11767         (when (= (+ to 2) from)
11768           (setq from beg)
11769           (setq to end))
11770         (if gnus-newsgroup-selected-overlay
11771             ;; Move old overlay.
11772             (gnus-move-overlay
11773              gnus-newsgroup-selected-overlay from to (current-buffer))
11774           ;; Create new overlay.
11775           (gnus-overlay-put
11776            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11777            'face gnus-summary-selected-face))))))
11778
11779 (defvar gnus-summary-highlight-line-cached nil)
11780 (defvar gnus-summary-highlight-line-trigger nil)
11781
11782 (defun gnus-summary-highlight-line-0 ()
11783   (if (and (eq gnus-summary-highlight-line-trigger
11784                gnus-summary-highlight)
11785            gnus-summary-highlight-line-cached)
11786       gnus-summary-highlight-line-cached
11787     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11788           gnus-summary-highlight-line-cached
11789           (let* ((cond (list 'cond))
11790                  (c cond)
11791                  (list gnus-summary-highlight))
11792             (while list
11793               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11794                               nil))
11795               (setq c (cdr c)
11796                     list (cdr list)))
11797             (gnus-byte-compile (list 'lambda nil cond))))))
11798
11799 (defun gnus-summary-highlight-line ()
11800   "Highlight current line according to `gnus-summary-highlight'."
11801   (let* ((beg (point-at-bol))
11802          (article (or (gnus-summary-article-number) gnus-current-article))
11803          (score (or (cdr (assq article
11804                                gnus-newsgroup-scored))
11805                     gnus-summary-default-score 0))
11806          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11807          (inhibit-read-only t)
11808          (default gnus-summary-default-score)
11809          (default-high gnus-summary-default-high-score)
11810          (default-low gnus-summary-default-low-score)
11811          (uncached (and gnus-summary-use-undownloaded-faces
11812                         (memq article gnus-newsgroup-undownloaded)
11813                         (not (memq article gnus-newsgroup-cached)))))
11814     (let ((face (funcall (gnus-summary-highlight-line-0))))
11815       (unless (eq face (get-text-property beg 'face))
11816         (gnus-put-text-property-excluding-characters-with-faces
11817          beg (point-at-eol) 'face
11818          (setq face (if (boundp face) (symbol-value face) face)))
11819         (when gnus-summary-highlight-line-function
11820           (funcall gnus-summary-highlight-line-function article face))))))
11821
11822 (defun gnus-update-read-articles (group unread &optional compute)
11823   "Update the list of read articles in GROUP.
11824 UNREAD is a sorted list."
11825   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11826         (info (gnus-get-info group))
11827         (prev 1)
11828         read)
11829     (if (or (not info) (not active))
11830         ;; There is no info on this group if it was, in fact,
11831         ;; killed.  Gnus stores no information on killed groups, so
11832         ;; there's nothing to be done.
11833         ;; One could store the information somewhere temporarily,
11834         ;; perhaps...  Hmmm...
11835         ()
11836       ;; Remove any negative articles numbers.
11837       (while (and unread (< (car unread) 0))
11838         (setq unread (cdr unread)))
11839       ;; Remove any expired article numbers
11840       (while (and unread (< (car unread) (car active)))
11841         (setq unread (cdr unread)))
11842       ;; Compute the ranges of read articles by looking at the list of
11843       ;; unread articles.
11844       (while unread
11845         (when (/= (car unread) prev)
11846           (push (if (= prev (1- (car unread))) prev
11847                   (cons prev (1- (car unread))))
11848                 read))
11849         (setq prev (1+ (car unread)))
11850         (setq unread (cdr unread)))
11851       (when (<= prev (cdr active))
11852         (push (cons prev (cdr active)) read))
11853       (setq read (if (> (length read) 1) (nreverse read) read))
11854       (if compute
11855           read
11856         (save-excursion
11857           (let (setmarkundo)
11858             ;; Propagate the read marks to the backend.
11859             (when (gnus-check-backend-function 'request-set-mark group)
11860               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11861                     (add (gnus-remove-from-range read (gnus-info-read info))))
11862                 (when (or add del)
11863                   (unless (gnus-check-group group)
11864                     (error "Can't open server for %s" group))
11865                   (gnus-request-set-mark
11866                    group (delq nil (list (if add (list add 'add '(read)))
11867                                          (if del (list del 'del '(read))))))
11868                   (setq setmarkundo
11869                         `(gnus-request-set-mark
11870                           ,group
11871                           ',(delq nil (list
11872                                        (if del (list del 'add '(read)))
11873                                        (if add (list add 'del '(read))))))))))
11874             (set-buffer gnus-group-buffer)
11875             (gnus-undo-register
11876               `(progn
11877                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11878                  (gnus-info-set-read ',info ',(gnus-info-read info))
11879                  (gnus-get-unread-articles-in-group ',info
11880                                                     (gnus-active ,group))
11881                  (gnus-group-update-group ,group t)
11882                  ,setmarkundo))))
11883         ;; Enter this list into the group info.
11884         (gnus-info-set-read info read)
11885         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11886         (gnus-get-unread-articles-in-group info (gnus-active group))
11887         t))))
11888
11889 (defun gnus-offer-save-summaries ()
11890   "Offer to save all active summary buffers."
11891   (let (buffers)
11892     ;; Go through all buffers and find all summaries.
11893     (dolist (buffer (buffer-list))
11894       (when (and (setq buffer (buffer-name buffer))
11895                  (string-match "Summary" buffer)
11896                  (with-current-buffer buffer
11897                    ;; We check that this is, indeed, a summary buffer.
11898                    (and (eq major-mode 'gnus-summary-mode)
11899                         ;; Also make sure this isn't bogus.
11900                         gnus-newsgroup-prepared
11901                         ;; Also make sure that this isn't a
11902                         ;; dead summary buffer.
11903                         (not gnus-dead-summary-mode))))
11904         (push buffer buffers)))
11905     ;; Go through all these summary buffers and offer to save them.
11906     (when buffers
11907       (save-excursion
11908         (map-y-or-n-p
11909          "Update summary buffer %s? "
11910          (lambda (buf)
11911            (switch-to-buffer buf)
11912            (gnus-summary-exit))
11913          buffers)))))
11914
11915 (defun gnus-summary-setup-default-charset ()
11916   "Setup newsgroup default charset."
11917   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11918       (setq gnus-newsgroup-charset nil)
11919     (let* ((ignored-charsets
11920             (or gnus-newsgroup-ephemeral-ignored-charsets
11921                 (append
11922                  (and gnus-newsgroup-name
11923                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11924                  gnus-newsgroup-ignored-charsets))))
11925       (setq gnus-newsgroup-charset
11926             (or gnus-newsgroup-ephemeral-charset
11927                 (and gnus-newsgroup-name
11928                      (gnus-parameter-charset gnus-newsgroup-name))
11929                 gnus-default-charset))
11930       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11931            ignored-charsets))))
11932
11933 ;;;
11934 ;;; Mime Commands
11935 ;;;
11936
11937 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11938   "Display the current article buffer fully MIME-buttonized.
11939 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11940 treated as multipart/mixed."
11941   (interactive "P")
11942   (require 'gnus-art)
11943   (let ((gnus-unbuttonized-mime-types nil)
11944         (gnus-mime-display-multipart-as-mixed show-all-parts))
11945     (gnus-summary-show-article)))
11946
11947 (defun gnus-summary-repair-multipart (article)
11948   "Add a Content-Type header to a multipart article without one."
11949   (interactive (list (gnus-summary-article-number)))
11950   (gnus-with-article article
11951     (message-narrow-to-head)
11952     (message-remove-header "Mime-Version")
11953     (goto-char (point-max))
11954     (insert "Mime-Version: 1.0\n")
11955     (widen)
11956     (when (search-forward "\n--" nil t)
11957       (let ((separator (buffer-substring (point) (point-at-eol))))
11958         (message-narrow-to-head)
11959         (message-remove-header "Content-Type")
11960         (goto-char (point-max))
11961         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11962                         separator))
11963         (widen))))
11964   (let (gnus-mark-article-hook)
11965     (gnus-summary-select-article t t nil article)))
11966
11967 (defun gnus-summary-toggle-display-buttonized ()
11968   "Toggle the buttonizing of the article buffer."
11969   (interactive)
11970   (require 'gnus-art)
11971   (if (setq gnus-inhibit-mime-unbuttonizing
11972             (not gnus-inhibit-mime-unbuttonizing))
11973       (let ((gnus-unbuttonized-mime-types nil))
11974         (gnus-summary-show-article))
11975     (gnus-summary-show-article)))
11976
11977 ;;;
11978 ;;; Generic summary marking commands
11979 ;;;
11980
11981 (defvar gnus-summary-marking-alist
11982   '((read gnus-del-mark "d")
11983     (unread gnus-unread-mark "u")
11984     (ticked gnus-ticked-mark "!")
11985     (dormant gnus-dormant-mark "?")
11986     (expirable gnus-expirable-mark "e"))
11987   "An alist of names/marks/keystrokes.")
11988
11989 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11990 (defvar gnus-summary-mark-map)
11991
11992 (defun gnus-summary-make-all-marking-commands ()
11993   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11994   (dolist (elem gnus-summary-marking-alist)
11995     (apply 'gnus-summary-make-marking-command elem)))
11996
11997 (defun gnus-summary-make-marking-command (name mark keystroke)
11998   (let ((map (make-sparse-keymap)))
11999     (define-key gnus-summary-generic-mark-map keystroke map)
12000     (dolist (lway `((next "next" next nil "n")
12001                     (next-unread "next unread" next t "N")
12002                     (prev "previous" prev nil "p")
12003                     (prev-unread "previous unread" prev t "P")
12004                     (nomove "" nil nil ,keystroke)))
12005       (let ((func (gnus-summary-make-marking-command-1
12006                    mark (car lway) lway name)))
12007         (setq func (eval func))
12008         (define-key map (nth 4 lway) func)))))
12009
12010 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12011   `(defun ,(intern
12012             (format "gnus-summary-put-mark-as-%s%s"
12013                     name (if (eq way 'nomove)
12014                              ""
12015                            (concat "-" (symbol-name way)))))
12016      (n)
12017      ,(format
12018        "Mark the current article as %s%s.
12019 If N, the prefix, then repeat N times.
12020 If N is negative, move in reverse order.
12021 The difference between N and the actual number of articles marked is
12022 returned."
12023        name (cadr lway))
12024      (interactive "p")
12025      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12026
12027 (defun gnus-summary-generic-mark (n mark move unread)
12028   "Mark N articles with MARK."
12029   (unless (eq major-mode 'gnus-summary-mode)
12030     (error "This command can only be used in the summary buffer"))
12031   (gnus-summary-show-thread)
12032   (let ((nummove
12033          (cond
12034           ((eq move 'next) 1)
12035           ((eq move 'prev) -1)
12036           (t 0))))
12037     (if (zerop nummove)
12038         (setq n 1)
12039       (when (< n 0)
12040         (setq n (abs n)
12041               nummove (* -1 nummove))))
12042     (while (and (> n 0)
12043                 (gnus-summary-mark-article nil mark)
12044                 (zerop (gnus-summary-next-subject nummove unread t)))
12045       (setq n (1- n)))
12046     (when (/= 0 n)
12047       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12048     (gnus-summary-recenter)
12049     (gnus-summary-position-point)
12050     (gnus-set-mode-line 'summary)
12051     n))
12052
12053 (defun gnus-summary-insert-articles (articles)
12054   (when (setq articles
12055               (gnus-sorted-difference articles
12056                                       (mapcar (lambda (h)
12057                                                 (mail-header-number h))
12058                                               gnus-newsgroup-headers)))
12059     (setq gnus-newsgroup-headers
12060           (gnus-merge 'list
12061                       gnus-newsgroup-headers
12062                       (gnus-fetch-headers articles)
12063                       'gnus-article-sort-by-number))
12064     ;; Suppress duplicates?
12065     (when gnus-suppress-duplicates
12066       (gnus-dup-suppress-articles))
12067
12068     ;; We might want to build some more threads first.
12069     (when (and gnus-fetch-old-headers
12070                (eq gnus-headers-retrieved-by 'nov))
12071       (if (eq gnus-fetch-old-headers 'invisible)
12072           (gnus-build-all-threads)
12073         (gnus-build-old-threads)))
12074     ;; Let the Gnus agent mark articles as read.
12075     (when gnus-agent
12076       (gnus-agent-get-undownloaded-list))
12077     ;; Remove list identifiers from subject
12078     (when gnus-list-identifiers
12079       (gnus-summary-remove-list-identifiers))
12080     ;; First and last article in this newsgroup.
12081     (when gnus-newsgroup-headers
12082       (setq gnus-newsgroup-begin
12083             (mail-header-number (car gnus-newsgroup-headers))
12084             gnus-newsgroup-end
12085             (mail-header-number
12086              (gnus-last-element gnus-newsgroup-headers))))
12087     (when gnus-use-scoring
12088       (gnus-possibly-score-headers))))
12089
12090 (defun gnus-summary-insert-old-articles (&optional all)
12091   "Insert all old articles in this group.
12092 If ALL is non-nil, already read articles become readable.
12093 If ALL is a number, fetch this number of articles."
12094   (interactive "P")
12095   (prog1
12096       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12097             older len)
12098         (setq older
12099               ;; Some nntp servers lie about their active range.  When
12100               ;; this happens, the active range can be in the millions.
12101               ;; Use a compressed range to avoid creating a huge list.
12102               (gnus-range-difference (list gnus-newsgroup-active) old))
12103         (setq len (gnus-range-length older))
12104         (cond
12105          ((null older) nil)
12106          ((numberp all)
12107           (if (< all len)
12108               (let ((older-range (nreverse older)))
12109                 (setq older nil)
12110
12111                 (while (> all 0)
12112                   (let* ((r (pop older-range))
12113                          (min (if (numberp r) r (car r)))
12114                          (max (if (numberp r) r (cdr r))))
12115                     (while (and (<= min max)
12116                                 (> all 0))
12117                       (push max older)
12118                       (setq all (1- all)
12119                             max (1- max))))))
12120             (setq older (gnus-uncompress-range older))))
12121          (all
12122           (setq older (gnus-uncompress-range older)))
12123          (t
12124           (when (and (numberp gnus-large-newsgroup)
12125                    (> len gnus-large-newsgroup))
12126               (let* ((cursor-in-echo-area nil)
12127                      (initial (gnus-parameter-large-newsgroup-initial
12128                                gnus-newsgroup-name))
12129                      (input
12130                       (read-string
12131                        (format
12132                         "How many articles from %s (%s %d): "
12133                         (gnus-limit-string
12134                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
12135                         (if initial "max" "default")
12136                         len)
12137                        (if initial
12138                            (cons (number-to-string initial)
12139                                  0)))))
12140                 (unless (string-match "^[ \t]*$" input)
12141                   (setq all (string-to-number input))
12142                   (if (< all len)
12143                       (let ((older-range (nreverse older)))
12144                         (setq older nil)
12145
12146                         (while (> all 0)
12147                           (let* ((r (pop older-range))
12148                                  (min (if (numberp r) r (car r)))
12149                                  (max (if (numberp r) r (cdr r))))
12150                             (while (and (<= min max)
12151                                         (> all 0))
12152                               (push max older)
12153                               (setq all (1- all)
12154                                     max (1- max))))))))))
12155           (setq older (gnus-uncompress-range older))))
12156         (if (not older)
12157             (message "No old news.")
12158           (gnus-summary-insert-articles older)
12159           (gnus-summary-limit (gnus-sorted-nunion old older))))
12160     (gnus-summary-position-point)))
12161
12162 (defun gnus-summary-insert-new-articles ()
12163   "Insert all new articles in this group."
12164   (interactive)
12165   (prog1
12166       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12167             (old-active gnus-newsgroup-active)
12168             (nnmail-fetched-sources (list t))
12169             i new)
12170         (setq gnus-newsgroup-active
12171               (gnus-activate-group gnus-newsgroup-name 'scan))
12172         (setq i (cdr gnus-newsgroup-active))
12173         (while (> i (cdr old-active))
12174           (push i new)
12175           (decf i))
12176         (if (not new)
12177             (message "No gnus is bad news")
12178           (gnus-summary-insert-articles new)
12179           (setq gnus-newsgroup-unreads
12180                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12181           (gnus-summary-limit (gnus-sorted-nunion old new))))
12182     (gnus-summary-position-point)))
12183
12184 (gnus-summary-make-all-marking-commands)
12185
12186 (gnus-ems-redefine)
12187
12188 (provide 'gnus-sum)
12189
12190 (run-hooks 'gnus-sum-load-hook)
12191
12192 ;; Local Variables:
12193 ;; coding: iso-8859-1
12194 ;; End:
12195
12196 ;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
12197 ;;; gnus-sum.el ends here