* gmm-utils.el (gmm-image-load-path-for-library): Sync with
[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 200
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   "Variable that says which function should be used to decode a string with encoded words.")
1056
1057 (defcustom gnus-extra-headers '(To Newsgroups)
1058   "*Extra headers to parse."
1059   :version "21.1"
1060   :group 'gnus-summary
1061   :type '(repeat symbol))
1062
1063 (defcustom gnus-ignored-from-addresses
1064   (and user-mail-address  
1065        (not (string= user-mail-address ""))
1066        (regexp-quote user-mail-address))
1067   "*Regexp of From headers that may be suppressed in favor of To headers."
1068   :version "21.1"
1069   :group 'gnus-summary
1070   :type 'regexp)
1071
1072 (defcustom gnus-summary-to-prefix "-> "
1073   "*String prefixed to the To field in the summary line when
1074 using `gnus-ignored-from-addresses'."
1075   :version "22.1"
1076   :group 'gnus-summary
1077   :type 'string)
1078
1079 (defcustom gnus-summary-newsgroup-prefix "=> "
1080   "*String prefixed to the Newsgroup field in the summary
1081 line when using `gnus-ignored-from-addresses'."
1082   :version "22.1"
1083   :group 'gnus-summary
1084   :type 'string)
1085
1086 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1087   "List of charsets that should be ignored.
1088 When these charsets are used in the \"charset\" parameter, the
1089 default charset will be used instead."
1090   :version "21.1"
1091   :type '(repeat symbol)
1092   :group 'gnus-charset)
1093
1094 (gnus-define-group-parameter
1095  ignored-charsets
1096  :type list
1097  :function-document
1098  "Return the ignored charsets of GROUP."
1099  :variable gnus-group-ignored-charsets-alist
1100  :variable-default
1101  '(("alt\\.chinese\\.text" iso-8859-1))
1102  :variable-document
1103  "Alist of regexps (to match group names) and charsets that should be ignored.
1104 When these charsets are used in the \"charset\" parameter, the
1105 default charset will be used instead."
1106  :variable-group gnus-charset
1107  :variable-type '(repeat (cons (regexp :tag "Group")
1108                                (repeat symbol)))
1109  :parameter-type '(choice :tag "Ignored charsets"
1110                           :value nil
1111                           (repeat (symbol)))
1112  :parameter-document       "\
1113 List of charsets that should be ignored.
1114
1115 When these charsets are used in the \"charset\" parameter, the
1116 default charset will be used instead.")
1117
1118 (defcustom gnus-group-highlight-words-alist nil
1119   "Alist of group regexps and highlight regexps.
1120 This variable uses the same syntax as `gnus-emphasis-alist'."
1121   :version "21.1"
1122   :type '(repeat (cons (regexp :tag "Group")
1123                        (repeat (list (regexp :tag "Highlight regexp")
1124                                      (number :tag "Group for entire word" 0)
1125                                      (number :tag "Group for displayed part" 0)
1126                                      (symbol :tag "Face"
1127                                              gnus-emphasis-highlight-words)))))
1128   :group 'gnus-summary-visual)
1129
1130 (defcustom gnus-summary-show-article-charset-alist
1131   nil
1132   "Alist of number and charset.
1133 The article will be shown with the charset corresponding to the
1134 numbered argument.
1135 For example: ((1 . cn-gb-2312) (2 . big5))."
1136   :version "21.1"
1137   :type '(repeat (cons (number :tag "Argument" 1)
1138                        (symbol :tag "Charset")))
1139   :group 'gnus-charset)
1140
1141 (defcustom gnus-preserve-marks t
1142   "Whether marks are preserved when moving, copying and respooling messages."
1143   :version "21.1"
1144   :type 'boolean
1145   :group 'gnus-summary-marks)
1146
1147 (defcustom gnus-alter-articles-to-read-function nil
1148   "Function to be called to alter the list of articles to be selected."
1149   :type '(choice (const nil) function)
1150   :group 'gnus-summary)
1151
1152 (defcustom gnus-orphan-score nil
1153   "*All orphans get this score added.  Set in the score file."
1154   :group 'gnus-score-default
1155   :type '(choice (const nil)
1156                  integer))
1157
1158 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1159   "*A regexp to match MIME parts when saving multiple parts of a
1160 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1161 This regexp will be used by default when prompting the user for which
1162 type of files to save."
1163   :group 'gnus-summary
1164   :type 'regexp)
1165
1166 (defcustom gnus-read-all-available-headers nil
1167   "Whether Gnus should parse all headers made available to it.
1168 This is mostly relevant for slow back ends where the user may
1169 wish to widen the summary buffer to include all headers
1170 that were fetched.  Say, for nnultimate groups."
1171   :version "22.1"
1172   :group 'gnus-summary
1173   :type '(choice boolean regexp))
1174
1175 (defcustom gnus-summary-muttprint-program "muttprint"
1176   "Command (and optional arguments) used to run Muttprint."
1177   :version "22.1"
1178   :group 'gnus-summary
1179   :type 'string)
1180
1181 (defcustom gnus-article-loose-mime t
1182   "If non-nil, don't require MIME-Version header.
1183 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1184 supply the MIME-Version header or deliberately strip it from the mail.
1185 If non-nil (the default), Gnus will treat some articles as MIME
1186 even if the MIME-Version header is missing."
1187   :version "22.1"
1188   :type 'boolean
1189   :group 'gnus-article-mime)
1190
1191 (defcustom gnus-article-emulate-mime t
1192   "If non-nil, use MIME emulation for uuencode and the like.
1193 This means that Gnus will search message bodies for text that look
1194 like uuencoded bits, yEncoded bits, and so on, and present that using
1195 the normal Gnus MIME machinery."
1196   :version "22.1"
1197   :type 'boolean
1198   :group 'gnus-article-mime)
1199
1200 ;;; Internal variables
1201
1202 (defvar gnus-summary-display-cache nil)
1203 (defvar gnus-article-mime-handles nil)
1204 (defvar gnus-article-decoded-p nil)
1205 (defvar gnus-article-charset nil)
1206 (defvar gnus-article-ignored-charsets nil)
1207 (defvar gnus-scores-exclude-files nil)
1208 (defvar gnus-page-broken nil)
1209
1210 (defvar gnus-original-article nil)
1211 (defvar gnus-article-internal-prepare-hook nil)
1212 (defvar gnus-newsgroup-process-stack nil)
1213
1214 (defvar gnus-thread-indent-array nil)
1215 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1216 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1217   "Function called to sort the articles within a thread after it has been gathered together.")
1218
1219 (defvar gnus-summary-save-parts-type-history nil)
1220 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1221
1222 ;; Avoid highlighting in kill files.
1223 (defvar gnus-summary-inhibit-highlight nil)
1224 (defvar gnus-newsgroup-selected-overlay nil)
1225 (defvar gnus-inhibit-limiting nil)
1226 (defvar gnus-newsgroup-adaptive-score-file nil)
1227 (defvar gnus-current-score-file nil)
1228 (defvar gnus-current-move-group nil)
1229 (defvar gnus-current-copy-group nil)
1230 (defvar gnus-current-crosspost-group nil)
1231 (defvar gnus-newsgroup-display nil)
1232
1233 (defvar gnus-newsgroup-dependencies nil)
1234 (defvar gnus-newsgroup-adaptive nil)
1235 (defvar gnus-summary-display-article-function nil)
1236 (defvar gnus-summary-highlight-line-function nil
1237   "Function called after highlighting a summary line.")
1238
1239 (defvar gnus-summary-line-format-alist
1240   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1241     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1242     (?s gnus-tmp-subject-or-nil ?s)
1243     (?n gnus-tmp-name ?s)
1244     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1245         ?s)
1246     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1247             gnus-tmp-from) ?s)
1248     (?F gnus-tmp-from ?s)
1249     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1250     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1251     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1252     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1253     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1254     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1255     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1256     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1257     (?L gnus-tmp-lines ?s)
1258     (?O gnus-tmp-downloaded ?c)
1259     (?I gnus-tmp-indentation ?s)
1260     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1261     (?R gnus-tmp-replied ?c)
1262     (?\[ gnus-tmp-opening-bracket ?c)
1263     (?\] gnus-tmp-closing-bracket ?c)
1264     (?\> (make-string gnus-tmp-level ? ) ?s)
1265     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1266     (?i gnus-tmp-score ?d)
1267     (?z gnus-tmp-score-char ?c)
1268     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1269     (?U gnus-tmp-unread ?c)
1270     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1271         ?s)
1272     (?t (gnus-summary-number-of-articles-in-thread
1273          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1274         ?d)
1275     (?e (gnus-summary-number-of-articles-in-thread
1276          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1277         ?c)
1278     (?u gnus-tmp-user-defined ?s)
1279     (?P (gnus-pick-line-number) ?d)
1280     (?B gnus-tmp-thread-tree-header-string ?s)
1281     (user-date (gnus-user-date
1282                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1283   "An alist of format specifications that can appear in summary lines.
1284 These are paired with what variables they correspond with, along with
1285 the type of the variable (string, integer, character, etc).")
1286
1287 (defvar gnus-summary-dummy-line-format-alist
1288   `((?S gnus-tmp-subject ?s)
1289     (?N gnus-tmp-number ?d)
1290     (?u gnus-tmp-user-defined ?s)))
1291
1292 (defvar gnus-summary-mode-line-format-alist
1293   `((?G gnus-tmp-group-name ?s)
1294     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1295     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1296     (?A gnus-tmp-article-number ?d)
1297     (?Z gnus-tmp-unread-and-unselected ?s)
1298     (?V gnus-version ?s)
1299     (?U gnus-tmp-unread-and-unticked ?d)
1300     (?S gnus-tmp-subject ?s)
1301     (?e gnus-tmp-unselected ?d)
1302     (?u gnus-tmp-user-defined ?s)
1303     (?d (length gnus-newsgroup-dormant) ?d)
1304     (?t (length gnus-newsgroup-marked) ?d)
1305     (?h (length gnus-newsgroup-spam-marked) ?d)
1306     (?r (length gnus-newsgroup-reads) ?d)
1307     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1308     (?E gnus-newsgroup-expunged-tally ?d)
1309     (?s (gnus-current-score-file-nondirectory) ?s)))
1310
1311 (defvar gnus-last-search-regexp nil
1312   "Default regexp for article search command.")
1313
1314 (defvar gnus-last-shell-command nil
1315   "Default shell command on article.")
1316
1317 (defvar gnus-newsgroup-agentized nil
1318   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1319 (defvar gnus-newsgroup-begin nil)
1320 (defvar gnus-newsgroup-end nil)
1321 (defvar gnus-newsgroup-last-rmail nil)
1322 (defvar gnus-newsgroup-last-mail nil)
1323 (defvar gnus-newsgroup-last-folder nil)
1324 (defvar gnus-newsgroup-last-file nil)
1325 (defvar gnus-newsgroup-auto-expire nil)
1326 (defvar gnus-newsgroup-active nil)
1327
1328 (defvar gnus-newsgroup-data nil)
1329 (defvar gnus-newsgroup-data-reverse nil)
1330 (defvar gnus-newsgroup-limit nil)
1331 (defvar gnus-newsgroup-limits nil)
1332 (defvar gnus-summary-use-undownloaded-faces nil)
1333
1334 (defvar gnus-newsgroup-unreads nil
1335   "Sorted list of unread articles in the current newsgroup.")
1336
1337 (defvar gnus-newsgroup-unselected nil
1338   "Sorted list of unselected unread articles in the current newsgroup.")
1339
1340 (defvar gnus-newsgroup-reads nil
1341   "Alist of read articles and article marks in the current newsgroup.")
1342
1343 (defvar gnus-newsgroup-expunged-tally nil)
1344
1345 (defvar gnus-newsgroup-marked nil
1346   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1347
1348 (defvar gnus-newsgroup-spam-marked nil
1349   "List of ranges of articles that have been marked as spam.")
1350
1351 (defvar gnus-newsgroup-killed nil
1352   "List of ranges of articles that have been through the scoring process.")
1353
1354 (defvar gnus-newsgroup-cached nil
1355   "Sorted list of articles that come from the article cache.")
1356
1357 (defvar gnus-newsgroup-saved nil
1358   "List of articles that have been saved.")
1359
1360 (defvar gnus-newsgroup-kill-headers nil)
1361
1362 (defvar gnus-newsgroup-replied nil
1363   "List of articles that have been replied to in the current newsgroup.")
1364
1365 (defvar gnus-newsgroup-forwarded nil
1366   "List of articles that have been forwarded in the current newsgroup.")
1367
1368 (defvar gnus-newsgroup-recent nil
1369   "List of articles that have are recent in the current newsgroup.")
1370
1371 (defvar gnus-newsgroup-expirable nil
1372   "Sorted list of articles in the current newsgroup that can be expired.")
1373
1374 (defvar gnus-newsgroup-processable nil
1375   "List of articles in the current newsgroup that can be processed.")
1376
1377 (defvar gnus-newsgroup-downloadable nil
1378   "Sorted list of articles in the current newsgroup that can be processed.")
1379
1380 (defvar gnus-newsgroup-unfetched nil
1381   "Sorted list of articles in the current newsgroup whose headers have
1382 not been fetched into the agent.
1383
1384 This list will always be a subset of gnus-newsgroup-undownloaded.")
1385
1386 (defvar gnus-newsgroup-undownloaded nil
1387   "List of articles in the current newsgroup that haven't been downloaded.")
1388
1389 (defvar gnus-newsgroup-unsendable nil
1390   "List of articles in the current newsgroup that won't be sent.")
1391
1392 (defvar gnus-newsgroup-bookmarks nil
1393   "List of articles in the current newsgroup that have bookmarks.")
1394
1395 (defvar gnus-newsgroup-dormant nil
1396   "Sorted list of dormant articles in the current newsgroup.")
1397
1398 (defvar gnus-newsgroup-unseen nil
1399   "List of unseen articles in the current newsgroup.")
1400
1401 (defvar gnus-newsgroup-seen nil
1402   "Range of seen articles in the current newsgroup.")
1403
1404 (defvar gnus-newsgroup-articles nil
1405   "List of articles in the current newsgroup.")
1406
1407 (defvar gnus-newsgroup-scored nil
1408   "List of scored articles in the current newsgroup.")
1409
1410 (defvar gnus-newsgroup-headers nil
1411   "List of article headers in the current newsgroup.")
1412
1413 (defvar gnus-newsgroup-threads nil)
1414
1415 (defvar gnus-newsgroup-prepared nil
1416   "Whether the current group has been prepared properly.")
1417
1418 (defvar gnus-newsgroup-ancient nil
1419   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1420
1421 (defvar gnus-newsgroup-sparse nil)
1422
1423 (defvar gnus-current-article nil)
1424 (defvar gnus-article-current nil)
1425 (defvar gnus-current-headers nil)
1426 (defvar gnus-have-all-headers nil)
1427 (defvar gnus-last-article nil)
1428 (defvar gnus-newsgroup-history nil)
1429 (defvar gnus-newsgroup-charset nil)
1430 (defvar gnus-newsgroup-ephemeral-charset nil)
1431 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1432
1433 (defvar gnus-article-before-search nil)
1434
1435 (defvar gnus-summary-local-variables
1436   '(gnus-newsgroup-name
1437     gnus-newsgroup-begin gnus-newsgroup-end
1438     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1439     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1440     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1441     gnus-newsgroup-unselected gnus-newsgroup-marked
1442     gnus-newsgroup-spam-marked
1443     gnus-newsgroup-reads gnus-newsgroup-saved
1444     gnus-newsgroup-replied gnus-newsgroup-forwarded
1445     gnus-newsgroup-recent
1446     gnus-newsgroup-expirable
1447     gnus-newsgroup-processable gnus-newsgroup-killed
1448     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1449     gnus-newsgroup-unfetched
1450     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1451     gnus-newsgroup-seen gnus-newsgroup-articles
1452     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1453     gnus-newsgroup-headers gnus-newsgroup-threads
1454     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1455     gnus-current-article gnus-current-headers gnus-have-all-headers
1456     gnus-last-article gnus-article-internal-prepare-hook
1457     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1458     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1459     gnus-thread-expunge-below
1460     gnus-score-alist gnus-current-score-file
1461     (gnus-summary-expunge-below . global)
1462     (gnus-summary-mark-below . global)
1463     (gnus-orphan-score . global)
1464     gnus-newsgroup-active gnus-scores-exclude-files
1465     gnus-newsgroup-history gnus-newsgroup-ancient
1466     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1467     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1468     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1469     (gnus-newsgroup-expunged-tally . 0)
1470     gnus-cache-removable-articles gnus-newsgroup-cached
1471     gnus-newsgroup-data gnus-newsgroup-data-reverse
1472     gnus-newsgroup-limit gnus-newsgroup-limits
1473     gnus-newsgroup-charset gnus-newsgroup-display
1474     gnus-summary-use-undownloaded-faces)
1475   "Variables that are buffer-local to the summary buffers.")
1476
1477 (defvar gnus-newsgroup-variables nil
1478   "A list of variables that have separate values in different newsgroups.
1479 A list of newsgroup (summary buffer) local variables, or cons of
1480 variables and their default expressions to be evalled (when the default
1481 values are not nil), that should be made global while the summary buffer
1482 is active.
1483
1484 Note: The default expressions will be evaluated (using function `eval')
1485 before assignment to the local variable rather than just assigned to it.
1486 If the default expression is the symbol `global', that symbol will not
1487 be evaluated but the global value of the local variable will be used
1488 instead.
1489
1490 These variables can be used to set variables in the group parameters
1491 while still allowing them to affect operations done in other buffers.
1492 For example:
1493
1494 \(setq gnus-newsgroup-variables
1495      '(message-use-followup-to
1496        (gnus-visible-headers .
1497          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1498 ")
1499
1500 ;; Byte-compiler warning.
1501 (eval-when-compile
1502   ;; Bind features so that require will believe that gnus-sum has
1503   ;; already been loaded (avoids infinite recursion)
1504   (let ((features (cons 'gnus-sum features)))
1505     ;; Several of the declarations in gnus-sum are needed to load the
1506     ;; following files. Right now, these definitions have been
1507     ;; compiled but not defined (evaluated).  We could either do a
1508     ;; eval-and-compile about all of the declarations or evaluate the
1509     ;; source file.
1510     (if (boundp 'gnus-newsgroup-variables)
1511         nil
1512       (load "gnus-sum.el" t t t))
1513     (require 'gnus)
1514     (require 'gnus-art)))
1515
1516 ;; MIME stuff.
1517
1518 (defvar gnus-decode-encoded-word-methods
1519   '(mail-decode-encoded-word-string)
1520   "List of methods used to decode encoded words.
1521
1522 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1523 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1524 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1525 whose names match REGEXP.
1526
1527 For example:
1528 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1529  mail-decode-encoded-word-string
1530  (\"chinese\" . rfc1843-decode-string))")
1531
1532 (defvar gnus-decode-encoded-word-methods-cache nil)
1533
1534 (defun gnus-multi-decode-encoded-word-string (string)
1535   "Apply the functions from `gnus-encoded-word-methods' that match."
1536   (unless (and gnus-decode-encoded-word-methods-cache
1537                (eq gnus-newsgroup-name
1538                    (car gnus-decode-encoded-word-methods-cache)))
1539     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1540     (mapcar (lambda (x)
1541               (if (symbolp x)
1542                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1543                 (if (and gnus-newsgroup-name
1544                          (string-match (car x) gnus-newsgroup-name))
1545                     (nconc gnus-decode-encoded-word-methods-cache
1546                            (list (cdr x))))))
1547             gnus-decode-encoded-word-methods))
1548   (let ((xlist gnus-decode-encoded-word-methods-cache))
1549     (pop xlist)
1550     (while xlist
1551       (setq string (funcall (pop xlist) string))))
1552   string)
1553
1554 ;; Subject simplification.
1555
1556 (defun gnus-simplify-whitespace (str)
1557   "Remove excessive whitespace from STR."
1558   ;; Multiple spaces.
1559   (while (string-match "[ \t][ \t]+" str)
1560     (setq str (concat (substring str 0 (match-beginning 0))
1561                         " "
1562                         (substring str (match-end 0)))))
1563   ;; Leading spaces.
1564   (when (string-match "^[ \t]+" str)
1565     (setq str (substring str (match-end 0))))
1566   ;; Trailing spaces.
1567   (when (string-match "[ \t]+$" str)
1568     (setq str (substring str 0 (match-beginning 0))))
1569   str)
1570
1571 (defun gnus-simplify-all-whitespace (str)
1572   "Remove all whitespace from STR."
1573   (while (string-match "[ \t\n]+" str)
1574     (setq str (replace-match "" nil nil str)))
1575   str)
1576
1577 (defsubst gnus-simplify-subject-re (subject)
1578   "Remove \"Re:\" from subject lines."
1579   (if (string-match message-subject-re-regexp subject)
1580       (substring subject (match-end 0))
1581     subject))
1582
1583 (defun gnus-simplify-subject (subject &optional re-only)
1584   "Remove `Re:' and words in parentheses.
1585 If RE-ONLY is non-nil, strip leading `Re:'s only."
1586   (let ((case-fold-search t))           ;Ignore case.
1587     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1588     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1589       (setq subject (substring subject (match-end 0))))
1590     ;; Remove uninteresting prefixes.
1591     (when (and (not re-only)
1592                gnus-simplify-ignored-prefixes
1593                (string-match gnus-simplify-ignored-prefixes subject))
1594       (setq subject (substring subject (match-end 0))))
1595     ;; Remove words in parentheses from end.
1596     (unless re-only
1597       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1598         (setq subject (substring subject 0 (match-beginning 0)))))
1599     ;; Return subject string.
1600     subject))
1601
1602 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1603 ;; all whitespace.
1604 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1605   (goto-char (point-min))
1606   (while (re-search-forward regexp nil t)
1607     (replace-match (or newtext ""))))
1608
1609 (defun gnus-simplify-buffer-fuzzy ()
1610   "Simplify string in the buffer fuzzily.
1611 The string in the accessible portion of the current buffer is simplified.
1612 It is assumed to be a single-line subject.
1613 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1614 matter is removed.  Additional things can be deleted by setting
1615 `gnus-simplify-subject-fuzzy-regexp'."
1616   (let ((case-fold-search t)
1617         (modified-tick))
1618     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1619
1620     (while (not (eq modified-tick (buffer-modified-tick)))
1621       (setq modified-tick (buffer-modified-tick))
1622       (cond
1623        ((listp gnus-simplify-subject-fuzzy-regexp)
1624         (mapcar 'gnus-simplify-buffer-fuzzy-step
1625                 gnus-simplify-subject-fuzzy-regexp))
1626        (gnus-simplify-subject-fuzzy-regexp
1627         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1628       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1629       (gnus-simplify-buffer-fuzzy-step
1630        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1631       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1632
1633     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1634     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1635     (gnus-simplify-buffer-fuzzy-step " $")
1636     (gnus-simplify-buffer-fuzzy-step "^ +")))
1637
1638 (defun gnus-simplify-subject-fuzzy (subject)
1639   "Simplify a subject string fuzzily.
1640 See `gnus-simplify-buffer-fuzzy' for details."
1641   (save-excursion
1642     (gnus-set-work-buffer)
1643     (let ((case-fold-search t))
1644       ;; Remove uninteresting prefixes.
1645       (when (and gnus-simplify-ignored-prefixes
1646                  (string-match gnus-simplify-ignored-prefixes subject))
1647         (setq subject (substring subject (match-end 0))))
1648       (insert subject)
1649       (inline (gnus-simplify-buffer-fuzzy))
1650       (buffer-string))))
1651
1652 (defsubst gnus-simplify-subject-fully (subject)
1653   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1654   (cond
1655    (gnus-simplify-subject-functions
1656     (gnus-map-function gnus-simplify-subject-functions subject))
1657    ((null gnus-summary-gather-subject-limit)
1658     (gnus-simplify-subject-re subject))
1659    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1660     (gnus-simplify-subject-fuzzy subject))
1661    ((numberp gnus-summary-gather-subject-limit)
1662     (gnus-limit-string (gnus-simplify-subject-re subject)
1663                        gnus-summary-gather-subject-limit))
1664    (t
1665     subject)))
1666
1667 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1668   "Check whether two subjects are equal.
1669 If optional argument SIMPLE-FIRST is t, first argument is already
1670 simplified."
1671   (cond
1672    ((null simple-first)
1673     (equal (gnus-simplify-subject-fully s1)
1674            (gnus-simplify-subject-fully s2)))
1675    (t
1676     (equal s1
1677            (gnus-simplify-subject-fully s2)))))
1678
1679 (defun gnus-summary-bubble-group ()
1680   "Increase the score of the current group.
1681 This is a handy function to add to `gnus-summary-exit-hook' to
1682 increase the score of each group you read."
1683   (gnus-group-add-score gnus-newsgroup-name))
1684
1685 \f
1686 ;;;
1687 ;;; Gnus summary mode
1688 ;;;
1689
1690 (put 'gnus-summary-mode 'mode-class 'special)
1691
1692 (defvar gnus-article-commands-menu)
1693
1694 ;; Non-orthogonal keys
1695
1696 (gnus-define-keys gnus-summary-mode-map
1697   " " gnus-summary-next-page
1698   "\177" gnus-summary-prev-page
1699   [delete] gnus-summary-prev-page
1700   [backspace] gnus-summary-prev-page
1701   "\r" gnus-summary-scroll-up
1702   "\M-\r" gnus-summary-scroll-down
1703   "n" gnus-summary-next-unread-article
1704   "p" gnus-summary-prev-unread-article
1705   "N" gnus-summary-next-article
1706   "P" gnus-summary-prev-article
1707   "\M-\C-n" gnus-summary-next-same-subject
1708   "\M-\C-p" gnus-summary-prev-same-subject
1709   "\M-n" gnus-summary-next-unread-subject
1710   "\M-p" gnus-summary-prev-unread-subject
1711   "." gnus-summary-first-unread-article
1712   "," gnus-summary-best-unread-article
1713   "\M-s" gnus-summary-search-article-forward
1714   "\M-r" gnus-summary-search-article-backward
1715   "<" gnus-summary-beginning-of-article
1716   ">" gnus-summary-end-of-article
1717   "j" gnus-summary-goto-article
1718   "^" gnus-summary-refer-parent-article
1719   "\M-^" gnus-summary-refer-article
1720   "u" gnus-summary-tick-article-forward
1721   "!" gnus-summary-tick-article-forward
1722   "U" gnus-summary-tick-article-backward
1723   "d" gnus-summary-mark-as-read-forward
1724   "D" gnus-summary-mark-as-read-backward
1725   "E" gnus-summary-mark-as-expirable
1726   "\M-u" gnus-summary-clear-mark-forward
1727   "\M-U" gnus-summary-clear-mark-backward
1728   "k" gnus-summary-kill-same-subject-and-select
1729   "\C-k" gnus-summary-kill-same-subject
1730   "\M-\C-k" gnus-summary-kill-thread
1731   "\M-\C-l" gnus-summary-lower-thread
1732   "e" gnus-summary-edit-article
1733   "#" gnus-summary-mark-as-processable
1734   "\M-#" gnus-summary-unmark-as-processable
1735   "\M-\C-t" gnus-summary-toggle-threads
1736   "\M-\C-s" gnus-summary-show-thread
1737   "\M-\C-h" gnus-summary-hide-thread
1738   "\M-\C-f" gnus-summary-next-thread
1739   "\M-\C-b" gnus-summary-prev-thread
1740   [(meta down)] gnus-summary-next-thread
1741   [(meta up)] gnus-summary-prev-thread
1742   "\M-\C-u" gnus-summary-up-thread
1743   "\M-\C-d" gnus-summary-down-thread
1744   "&" gnus-summary-execute-command
1745   "c" gnus-summary-catchup-and-exit
1746   "\C-w" gnus-summary-mark-region-as-read
1747   "\C-t" gnus-summary-toggle-truncation
1748   "?" gnus-summary-mark-as-dormant
1749   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1750   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1751   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1752   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1753   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1754   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1755   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1756   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1757   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1758   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1759   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1760   "=" gnus-summary-expand-window
1761   "\C-x\C-s" gnus-summary-reselect-current-group
1762   "\M-g" gnus-summary-rescan-group
1763   "w" gnus-summary-stop-page-breaking
1764   "\C-c\C-r" gnus-summary-caesar-message
1765   "f" gnus-summary-followup
1766   "F" gnus-summary-followup-with-original
1767   "C" gnus-summary-cancel-article
1768   "r" gnus-summary-reply
1769   "R" gnus-summary-reply-with-original
1770   "\C-c\C-f" gnus-summary-mail-forward
1771   "o" gnus-summary-save-article
1772   "\C-o" gnus-summary-save-article-mail
1773   "|" gnus-summary-pipe-output
1774   "\M-k" gnus-summary-edit-local-kill
1775   "\M-K" gnus-summary-edit-global-kill
1776   ;; "V" gnus-version
1777   "\C-c\C-d" gnus-summary-describe-group
1778   "q" gnus-summary-exit
1779   "Q" gnus-summary-exit-no-update
1780   "\C-c\C-i" gnus-info-find-node
1781   gnus-mouse-2 gnus-mouse-pick-article
1782   [follow-link] mouse-face
1783   "m" gnus-summary-mail-other-window
1784   "a" gnus-summary-post-news
1785   "i" gnus-summary-news-other-window
1786   "x" gnus-summary-limit-to-unread
1787   "s" gnus-summary-isearch-article
1788   "t" gnus-summary-toggle-header
1789   "g" gnus-summary-show-article
1790   "l" gnus-summary-goto-last-article
1791   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1792   "\C-d" gnus-summary-enter-digest-group
1793   "\M-\C-d" gnus-summary-read-document
1794   "\M-\C-e" gnus-summary-edit-parameters
1795   "\M-\C-a" gnus-summary-customize-parameters
1796   "\C-c\C-b" gnus-bug
1797   "*" gnus-cache-enter-article
1798   "\M-*" gnus-cache-remove-article
1799   "\M-&" gnus-summary-universal-argument
1800   "\C-l" gnus-recenter
1801   "I" gnus-summary-increase-score
1802   "L" gnus-summary-lower-score
1803   "\M-i" gnus-symbolic-argument
1804   "h" gnus-summary-select-article-buffer
1805
1806   "b" gnus-article-view-part
1807   "\M-t" gnus-summary-toggle-display-buttonized
1808
1809   "V" gnus-summary-score-map
1810   "X" gnus-uu-extract-map
1811   "S" gnus-summary-send-map)
1812
1813 ;; Sort of orthogonal keymap
1814 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1815   "t" gnus-summary-tick-article-forward
1816   "!" gnus-summary-tick-article-forward
1817   "d" gnus-summary-mark-as-read-forward
1818   "r" gnus-summary-mark-as-read-forward
1819   "c" gnus-summary-clear-mark-forward
1820   " " gnus-summary-clear-mark-forward
1821   "e" gnus-summary-mark-as-expirable
1822   "x" gnus-summary-mark-as-expirable
1823   "?" gnus-summary-mark-as-dormant
1824   "b" gnus-summary-set-bookmark
1825   "B" gnus-summary-remove-bookmark
1826   "#" gnus-summary-mark-as-processable
1827   "\M-#" gnus-summary-unmark-as-processable
1828   "S" gnus-summary-limit-include-expunged
1829   "C" gnus-summary-catchup
1830   "H" gnus-summary-catchup-to-here
1831   "h" gnus-summary-catchup-from-here
1832   "\C-c" gnus-summary-catchup-all
1833   "k" gnus-summary-kill-same-subject-and-select
1834   "K" gnus-summary-kill-same-subject
1835   "P" gnus-uu-mark-map)
1836
1837 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1838   "c" gnus-summary-clear-above
1839   "u" gnus-summary-tick-above
1840   "m" gnus-summary-mark-above
1841   "k" gnus-summary-kill-below)
1842
1843 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1844   "/" gnus-summary-limit-to-subject
1845   "n" gnus-summary-limit-to-articles
1846   "w" gnus-summary-pop-limit
1847   "s" gnus-summary-limit-to-subject
1848   "a" gnus-summary-limit-to-author
1849   "u" gnus-summary-limit-to-unread
1850   "m" gnus-summary-limit-to-marks
1851   "M" gnus-summary-limit-exclude-marks
1852   "v" gnus-summary-limit-to-score
1853   "*" gnus-summary-limit-include-cached
1854   "D" gnus-summary-limit-include-dormant
1855   "T" gnus-summary-limit-include-thread
1856   "d" gnus-summary-limit-exclude-dormant
1857   "t" gnus-summary-limit-to-age
1858   "." gnus-summary-limit-to-unseen
1859   "x" gnus-summary-limit-to-extra
1860   "p" gnus-summary-limit-to-display-predicate
1861   "E" gnus-summary-limit-include-expunged
1862   "c" gnus-summary-limit-exclude-childless-dormant
1863   "C" gnus-summary-limit-mark-excluded-as-read
1864   "o" gnus-summary-insert-old-articles
1865   "N" gnus-summary-insert-new-articles
1866   "r" gnus-summary-limit-to-replied
1867   "R" gnus-summary-limit-to-recipient)
1868
1869 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1870   "n" gnus-summary-next-unread-article
1871   "p" gnus-summary-prev-unread-article
1872   "N" gnus-summary-next-article
1873   "P" gnus-summary-prev-article
1874   "\C-n" gnus-summary-next-same-subject
1875   "\C-p" gnus-summary-prev-same-subject
1876   "\M-n" gnus-summary-next-unread-subject
1877   "\M-p" gnus-summary-prev-unread-subject
1878   "f" gnus-summary-first-unread-article
1879   "b" gnus-summary-best-unread-article
1880   "j" gnus-summary-goto-article
1881   "g" gnus-summary-goto-subject
1882   "l" gnus-summary-goto-last-article
1883   "o" gnus-summary-pop-article)
1884
1885 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1886   "k" gnus-summary-kill-thread
1887   "l" gnus-summary-lower-thread
1888   "i" gnus-summary-raise-thread
1889   "T" gnus-summary-toggle-threads
1890   "t" gnus-summary-rethread-current
1891   "^" gnus-summary-reparent-thread
1892   "s" gnus-summary-show-thread
1893   "S" gnus-summary-show-all-threads
1894   "h" gnus-summary-hide-thread
1895   "H" gnus-summary-hide-all-threads
1896   "n" gnus-summary-next-thread
1897   "p" gnus-summary-prev-thread
1898   "u" gnus-summary-up-thread
1899   "o" gnus-summary-top-thread
1900   "d" gnus-summary-down-thread
1901   "#" gnus-uu-mark-thread
1902   "\M-#" gnus-uu-unmark-thread)
1903
1904 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1905   "g" gnus-summary-prepare
1906   "c" gnus-summary-insert-cached-articles
1907   "d" gnus-summary-insert-dormant-articles)
1908
1909 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1910   "c" gnus-summary-catchup-and-exit
1911   "C" gnus-summary-catchup-all-and-exit
1912   "E" gnus-summary-exit-no-update
1913   "Q" gnus-summary-exit
1914   "Z" gnus-summary-exit
1915   "n" gnus-summary-catchup-and-goto-next-group
1916   "p" gnus-summary-catchup-and-goto-prev-group
1917   "R" gnus-summary-reselect-current-group
1918   "G" gnus-summary-rescan-group
1919   "N" gnus-summary-next-group
1920   "s" gnus-summary-save-newsrc
1921   "P" gnus-summary-prev-group)
1922
1923 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1924   " " gnus-summary-next-page
1925   "n" gnus-summary-next-page
1926   "\177" gnus-summary-prev-page
1927   [delete] gnus-summary-prev-page
1928   "p" gnus-summary-prev-page
1929   "\r" gnus-summary-scroll-up
1930   "\M-\r" gnus-summary-scroll-down
1931   "<" gnus-summary-beginning-of-article
1932   ">" gnus-summary-end-of-article
1933   "b" gnus-summary-beginning-of-article
1934   "e" gnus-summary-end-of-article
1935   "^" gnus-summary-refer-parent-article
1936   "r" gnus-summary-refer-parent-article
1937   "D" gnus-summary-enter-digest-group
1938   "R" gnus-summary-refer-references
1939   "T" gnus-summary-refer-thread
1940   "g" gnus-summary-show-article
1941   "s" gnus-summary-isearch-article
1942   "P" gnus-summary-print-article
1943   "M" gnus-mailing-list-insinuate
1944   "t" gnus-article-babel)
1945
1946 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1947   "b" gnus-article-add-buttons
1948   "B" gnus-article-add-buttons-to-head
1949   "o" gnus-article-treat-overstrike
1950   "e" gnus-article-emphasize
1951   "w" gnus-article-fill-cited-article
1952   "Q" gnus-article-fill-long-lines
1953   "C" gnus-article-capitalize-sentences
1954   "c" gnus-article-remove-cr
1955   "q" gnus-article-de-quoted-unreadable
1956   "6" gnus-article-de-base64-unreadable
1957   "Z" gnus-article-decode-HZ
1958   "A" gnus-article-treat-ansi-sequences
1959   "h" gnus-article-wash-html
1960   "u" gnus-article-unsplit-urls
1961   "s" gnus-summary-force-verify-and-decrypt
1962   "f" gnus-article-display-x-face
1963   "l" gnus-summary-stop-page-breaking
1964   "r" gnus-summary-caesar-message
1965   "m" gnus-summary-morse-message
1966   "t" gnus-summary-toggle-header
1967   "g" gnus-treat-smiley
1968   "v" gnus-summary-verbose-headers
1969   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1970   "p" gnus-article-verify-x-pgp-sig
1971   "d" gnus-article-treat-dumbquotes
1972   "i" gnus-summary-idna-message)
1973
1974 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1975   ;; mnemonic: deuglif*Y*
1976   "u" gnus-article-outlook-unwrap-lines
1977   "a" gnus-article-outlook-repair-attribution
1978   "c" gnus-article-outlook-rearrange-citation
1979   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1980
1981 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1982   "a" gnus-article-hide
1983   "h" gnus-article-hide-headers
1984   "b" gnus-article-hide-boring-headers
1985   "s" gnus-article-hide-signature
1986   "c" gnus-article-hide-citation
1987   "C" gnus-article-hide-citation-in-followups
1988   "l" gnus-article-hide-list-identifiers
1989   "B" gnus-article-strip-banner
1990   "P" gnus-article-hide-pem
1991   "\C-c" gnus-article-hide-citation-maybe)
1992
1993 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1994   "a" gnus-article-highlight
1995   "h" gnus-article-highlight-headers
1996   "c" gnus-article-highlight-citation
1997   "s" gnus-article-highlight-signature)
1998
1999 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2000   "f" gnus-article-treat-fold-headers
2001   "u" gnus-article-treat-unfold-headers
2002   "n" gnus-article-treat-fold-newsgroups)
2003
2004 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2005   "x" gnus-article-display-x-face
2006   "d" gnus-article-display-face
2007   "s" gnus-treat-smiley
2008   "D" gnus-article-remove-images
2009   "f" gnus-treat-from-picon
2010   "m" gnus-treat-mail-picon
2011   "n" gnus-treat-newsgroups-picon)
2012
2013 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2014   "w" gnus-article-decode-mime-words
2015   "c" gnus-article-decode-charset
2016   "v" gnus-mime-view-all-parts
2017   "b" gnus-article-view-part)
2018
2019 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2020   "z" gnus-article-date-ut
2021   "u" gnus-article-date-ut
2022   "l" gnus-article-date-local
2023   "p" gnus-article-date-english
2024   "e" gnus-article-date-lapsed
2025   "o" gnus-article-date-original
2026   "i" gnus-article-date-iso8601
2027   "s" gnus-article-date-user)
2028
2029 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2030   "t" gnus-article-remove-trailing-blank-lines
2031   "l" gnus-article-strip-leading-blank-lines
2032   "m" gnus-article-strip-multiple-blank-lines
2033   "a" gnus-article-strip-blank-lines
2034   "A" gnus-article-strip-all-blank-lines
2035   "s" gnus-article-strip-leading-space
2036   "e" gnus-article-strip-trailing-space
2037   "w" gnus-article-remove-leading-whitespace)
2038
2039 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2040   "v" gnus-version
2041   "f" gnus-summary-fetch-faq
2042   "d" gnus-summary-describe-group
2043   "h" gnus-summary-describe-briefly
2044   "i" gnus-info-find-node
2045   "c" gnus-group-fetch-charter
2046   "C" gnus-group-fetch-control)
2047
2048 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2049   "e" gnus-summary-expire-articles
2050   "\M-\C-e" gnus-summary-expire-articles-now
2051   "\177" gnus-summary-delete-article
2052   [delete] gnus-summary-delete-article
2053   [backspace] gnus-summary-delete-article
2054   "m" gnus-summary-move-article
2055   "r" gnus-summary-respool-article
2056   "w" gnus-summary-edit-article
2057   "c" gnus-summary-copy-article
2058   "B" gnus-summary-crosspost-article
2059   "q" gnus-summary-respool-query
2060   "t" gnus-summary-respool-trace
2061   "i" gnus-summary-import-article
2062   "I" gnus-summary-create-article
2063   "p" gnus-summary-article-posted-p)
2064
2065 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2066   "o" gnus-summary-save-article
2067   "m" gnus-summary-save-article-mail
2068   "F" gnus-summary-write-article-file
2069   "r" gnus-summary-save-article-rmail
2070   "f" gnus-summary-save-article-file
2071   "b" gnus-summary-save-article-body-file
2072   "h" gnus-summary-save-article-folder
2073   "v" gnus-summary-save-article-vm
2074   "p" gnus-summary-pipe-output
2075   "P" gnus-summary-muttprint
2076   "s" gnus-soup-add-article)
2077
2078 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2079   "b" gnus-summary-display-buttonized
2080   "m" gnus-summary-repair-multipart
2081   "v" gnus-article-view-part
2082   "o" gnus-article-save-part
2083   "O" gnus-article-save-part-and-strip
2084   "r" gnus-article-replace-part
2085   "d" gnus-article-delete-part
2086   "j" gnus-article-jump-to-part
2087   "c" gnus-article-copy-part
2088   "C" gnus-article-view-part-as-charset
2089   "e" gnus-article-view-part-externally
2090   "E" gnus-article-encrypt-body
2091   "i" gnus-article-inline-part
2092   "|" gnus-article-pipe-part)
2093
2094 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2095   "p" gnus-summary-mark-as-processable
2096   "u" gnus-summary-unmark-as-processable
2097   "U" gnus-summary-unmark-all-processable
2098   "v" gnus-uu-mark-over
2099   "s" gnus-uu-mark-series
2100   "r" gnus-uu-mark-region
2101   "g" gnus-uu-unmark-region
2102   "R" gnus-uu-mark-by-regexp
2103   "G" gnus-uu-unmark-by-regexp
2104   "t" gnus-uu-mark-thread
2105   "T" gnus-uu-unmark-thread
2106   "a" gnus-uu-mark-all
2107   "b" gnus-uu-mark-buffer
2108   "S" gnus-uu-mark-sparse
2109   "k" gnus-summary-kill-process-mark
2110   "y" gnus-summary-yank-process-mark
2111   "w" gnus-summary-save-process-mark
2112   "i" gnus-uu-invert-processable)
2113
2114 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2115   ;;"x" gnus-uu-extract-any
2116   "m" gnus-summary-save-parts
2117   "u" gnus-uu-decode-uu
2118   "U" gnus-uu-decode-uu-and-save
2119   "s" gnus-uu-decode-unshar
2120   "S" gnus-uu-decode-unshar-and-save
2121   "o" gnus-uu-decode-save
2122   "O" gnus-uu-decode-save
2123   "b" gnus-uu-decode-binhex
2124   "B" gnus-uu-decode-binhex
2125   "p" gnus-uu-decode-postscript
2126   "P" gnus-uu-decode-postscript-and-save)
2127
2128 (gnus-define-keys
2129     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2130   "u" gnus-uu-decode-uu-view
2131   "U" gnus-uu-decode-uu-and-save-view
2132   "s" gnus-uu-decode-unshar-view
2133   "S" gnus-uu-decode-unshar-and-save-view
2134   "o" gnus-uu-decode-save-view
2135   "O" gnus-uu-decode-save-view
2136   "b" gnus-uu-decode-binhex-view
2137   "B" gnus-uu-decode-binhex-view
2138   "p" gnus-uu-decode-postscript-view
2139   "P" gnus-uu-decode-postscript-and-save-view)
2140
2141 (defvar gnus-article-post-menu nil)
2142
2143 (defconst gnus-summary-menu-maxlen 20)
2144
2145 (defun gnus-summary-menu-split (menu)
2146   ;; If we have lots of elements, divide them into groups of 20
2147   ;; and make a pane (or submenu) for each one.
2148   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2149       (let ((menu menu) sublists next
2150             (i 1))
2151         (while menu
2152           ;; Pull off the next gnus-summary-menu-maxlen elements
2153           ;; and make them the next element of sublist.
2154           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2155           (if next
2156               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2157                       nil))
2158           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2159                                              (aref (car (last menu)) 0)) menu)
2160                                sublists))
2161           (setq i (1+ i))
2162           (setq menu next))
2163         (nreverse sublists))
2164     ;; Few elements--put them all in one pane.
2165     menu))
2166
2167 (defun gnus-summary-make-menu-bar ()
2168   (gnus-turn-off-edit-menu 'summary)
2169
2170   (unless (boundp 'gnus-summary-misc-menu)
2171
2172     (easy-menu-define
2173       gnus-summary-kill-menu gnus-summary-mode-map ""
2174       (cons
2175        "Score"
2176        (nconc
2177         (list
2178          ["Customize" gnus-score-customize t])
2179         (gnus-make-score-map 'increase)
2180         (gnus-make-score-map 'lower)
2181         '(("Mark"
2182            ["Kill below" gnus-summary-kill-below t]
2183            ["Mark above" gnus-summary-mark-above t]
2184            ["Tick above" gnus-summary-tick-above t]
2185            ["Clear above" gnus-summary-clear-above t])
2186           ["Current score" gnus-summary-current-score t]
2187           ["Set score" gnus-summary-set-score t]
2188           ["Switch current score file..." gnus-score-change-score-file t]
2189           ["Set mark below..." gnus-score-set-mark-below t]
2190           ["Set expunge below..." gnus-score-set-expunge-below t]
2191           ["Edit current score file" gnus-score-edit-current-scores t]
2192           ["Edit score file" gnus-score-edit-file t]
2193           ["Trace score" gnus-score-find-trace t]
2194           ["Find words" gnus-score-find-favourite-words t]
2195           ["Rescore buffer" gnus-summary-rescore t]
2196           ["Increase score..." gnus-summary-increase-score t]
2197           ["Lower score..." gnus-summary-lower-score t]))))
2198
2199     ;; Define both the Article menu in the summary buffer and the
2200     ;; equivalent Commands menu in the article buffer here for
2201     ;; consistency.
2202     (let ((innards
2203            `(("Hide"
2204               ["All" gnus-article-hide t]
2205               ["Headers" gnus-article-hide-headers t]
2206               ["Signature" gnus-article-hide-signature t]
2207               ["Citation" gnus-article-hide-citation t]
2208               ["List identifiers" gnus-article-hide-list-identifiers t]
2209               ["Banner" gnus-article-strip-banner t]
2210               ["Boring headers" gnus-article-hide-boring-headers t])
2211              ("Highlight"
2212               ["All" gnus-article-highlight t]
2213               ["Headers" gnus-article-highlight-headers t]
2214               ["Signature" gnus-article-highlight-signature t]
2215               ["Citation" gnus-article-highlight-citation t])
2216              ("MIME"
2217               ["Words" gnus-article-decode-mime-words t]
2218               ["Charset" gnus-article-decode-charset t]
2219               ["QP" gnus-article-de-quoted-unreadable t]
2220               ["Base64" gnus-article-de-base64-unreadable t]
2221               ["View MIME buttons" gnus-summary-display-buttonized t]
2222               ["View all" gnus-mime-view-all-parts t]
2223               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2224               ["Encrypt body" gnus-article-encrypt-body
2225                :active (not (gnus-group-read-only-p))
2226                ,@(if (featurep 'xemacs) nil
2227                    '(:help "Encrypt the message body on disk"))]
2228               ["Extract all parts..." gnus-summary-save-parts t]
2229               ("Multipart"
2230                ["Repair multipart" gnus-summary-repair-multipart t]
2231                ["Pipe part..." gnus-article-pipe-part t]
2232                ["Inline part" gnus-article-inline-part t]
2233                ["Encrypt body" gnus-article-encrypt-body
2234                 :active (not (gnus-group-read-only-p))
2235                ,@(if (featurep 'xemacs) nil
2236                    '(:help "Encrypt the message body on disk"))]
2237                ["View part externally" gnus-article-view-part-externally t]
2238                ["View part with charset..." gnus-article-view-part-as-charset t]
2239                ["Copy part" gnus-article-copy-part t]
2240                ["Save part..." gnus-article-save-part t]
2241                ["View part" gnus-article-view-part t]))
2242              ("Date"
2243               ["Local" gnus-article-date-local t]
2244               ["ISO8601" gnus-article-date-iso8601 t]
2245               ["UT" gnus-article-date-ut t]
2246               ["Original" gnus-article-date-original t]
2247               ["Lapsed" gnus-article-date-lapsed t]
2248               ["User-defined" gnus-article-date-user t])
2249              ("Display"
2250               ["Remove images" gnus-article-remove-images t]
2251               ["Toggle smiley" gnus-treat-smiley t]
2252               ["Show X-Face" gnus-article-display-x-face t]
2253               ["Show picons in From" gnus-treat-from-picon t]
2254               ["Show picons in mail headers" gnus-treat-mail-picon t]
2255               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2256               ("View as different encoding"
2257                ,@(gnus-summary-menu-split
2258                   (mapcar
2259                    (lambda (cs)
2260                      ;; Since easymenu under Emacs doesn't allow
2261                      ;; lambda forms for menu commands, we should
2262                      ;; provide intern'ed function symbols.
2263                      (let ((command (intern (format "\
2264 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2265                        (fset command
2266                              `(lambda ()
2267                                 (interactive)
2268                                 (let ((gnus-summary-show-article-charset-alist
2269                                        '((1 . ,cs))))
2270                                   (gnus-summary-show-article 1))))
2271                        `[,(symbol-name cs) ,command t]))
2272                    (sort (if (fboundp 'coding-system-list)
2273                              (coding-system-list)
2274                            (mapcar 'car mm-mime-mule-charset-alist))
2275                          'string<)))))
2276              ("Washing"
2277               ("Remove Blanks"
2278                ["Leading" gnus-article-strip-leading-blank-lines t]
2279                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2280                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2281                ["All of the above" gnus-article-strip-blank-lines t]
2282                ["All" gnus-article-strip-all-blank-lines t]
2283                ["Leading space" gnus-article-strip-leading-space t]
2284                ["Trailing space" gnus-article-strip-trailing-space t]
2285                ["Leading space in headers"
2286                 gnus-article-remove-leading-whitespace t])
2287               ["Overstrike" gnus-article-treat-overstrike t]
2288               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2289               ["Emphasis" gnus-article-emphasize t]
2290               ["Word wrap" gnus-article-fill-cited-article t]
2291               ["Fill long lines" gnus-article-fill-long-lines t]
2292               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2293               ["Remove CR" gnus-article-remove-cr t]
2294               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2295               ["Base64" gnus-article-de-base64-unreadable t]
2296               ["Rot 13" gnus-summary-caesar-message
2297                ,@(if (featurep 'xemacs) '(t)
2298                    '(:help "\"Caesar rotate\" article by 13"))]
2299               ["De-IDNA" gnus-summary-idna-message t]
2300               ["Morse decode" gnus-summary-morse-message t]
2301               ["Unix pipe..." gnus-summary-pipe-message t]
2302               ["Add buttons" gnus-article-add-buttons t]
2303               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2304               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2305               ["Verbose header" gnus-summary-verbose-headers t]
2306               ["Toggle header" gnus-summary-toggle-header t]
2307               ["Unfold headers" gnus-article-treat-unfold-headers t]
2308               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2309               ["Html" gnus-article-wash-html t]
2310               ["Unsplit URLs" gnus-article-unsplit-urls t]
2311               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2312               ["Decode HZ" gnus-article-decode-HZ t]
2313               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2314               ("(Outlook) Deuglify"
2315                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2316                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2317                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2318                ["Full (Outlook) deuglify"
2319                 gnus-article-outlook-deuglify-article t])
2320               )
2321              ("Output"
2322               ["Save in default format..." gnus-summary-save-article
2323                ,@(if (featurep 'xemacs) '(t)
2324                    '(:help "Save article using default method"))]
2325               ["Save in file..." gnus-summary-save-article-file
2326                ,@(if (featurep 'xemacs) '(t)
2327                    '(:help "Save article in file"))]
2328               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2329               ["Save in MH folder..." gnus-summary-save-article-folder t]
2330               ["Save in VM folder..." gnus-summary-save-article-vm t]
2331               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2332               ["Save body in file..." gnus-summary-save-article-body-file t]
2333               ["Pipe through a filter..." gnus-summary-pipe-output t]
2334               ["Add to SOUP packet" gnus-soup-add-article t]
2335               ["Print with Muttprint..." gnus-summary-muttprint t]
2336               ["Print" gnus-summary-print-article
2337                ,@(if (featurep 'xemacs) '(t)
2338                    '(:help "Generate and print a PostScript image"))])
2339              ("Copy, move,... (Backend)"
2340               ,@(if (featurep 'xemacs) nil
2341                   '(:help "Copying, moving, expiring articles..."))
2342               ["Respool article..." gnus-summary-respool-article t]
2343               ["Move article..." gnus-summary-move-article
2344                (gnus-check-backend-function
2345                 'request-move-article gnus-newsgroup-name)]
2346               ["Copy article..." gnus-summary-copy-article t]
2347               ["Crosspost article..." gnus-summary-crosspost-article
2348                (gnus-check-backend-function
2349                 'request-replace-article gnus-newsgroup-name)]
2350               ["Import file..." gnus-summary-import-article
2351                (gnus-check-backend-function
2352                 'request-accept-article gnus-newsgroup-name)]
2353               ["Create article..." gnus-summary-create-article
2354                (gnus-check-backend-function
2355                 'request-accept-article gnus-newsgroup-name)]
2356               ["Check if posted" gnus-summary-article-posted-p t]
2357               ["Edit article" gnus-summary-edit-article
2358                (not (gnus-group-read-only-p))]
2359               ["Delete article" gnus-summary-delete-article
2360                (gnus-check-backend-function
2361                 'request-expire-articles gnus-newsgroup-name)]
2362               ["Query respool" gnus-summary-respool-query t]
2363               ["Trace respool" gnus-summary-respool-trace t]
2364               ["Delete expirable articles" gnus-summary-expire-articles-now
2365                (gnus-check-backend-function
2366                 'request-expire-articles gnus-newsgroup-name)])
2367              ("Extract"
2368               ["Uudecode" gnus-uu-decode-uu
2369                ,@(if (featurep 'xemacs) '(t)
2370                    '(:help "Decode uuencoded article(s)"))]
2371               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2372               ["Unshar" gnus-uu-decode-unshar t]
2373               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2374               ["Save" gnus-uu-decode-save t]
2375               ["Binhex" gnus-uu-decode-binhex t]
2376               ["Postscript" gnus-uu-decode-postscript t]
2377               ["All MIME parts" gnus-summary-save-parts t])
2378              ("Cache"
2379               ["Enter article" gnus-cache-enter-article t]
2380               ["Remove article" gnus-cache-remove-article t])
2381              ["Translate" gnus-article-babel t]
2382              ["Select article buffer" gnus-summary-select-article-buffer t]
2383              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2384              ["Isearch article..." gnus-summary-isearch-article t]
2385              ["Beginning of the article" gnus-summary-beginning-of-article t]
2386              ["End of the article" gnus-summary-end-of-article t]
2387              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2388              ["Fetch referenced articles" gnus-summary-refer-references t]
2389              ["Fetch current thread" gnus-summary-refer-thread t]
2390              ["Fetch article with id..." gnus-summary-refer-article t]
2391              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2392              ["Redisplay" gnus-summary-show-article t]
2393              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2394       (easy-menu-define
2395         gnus-summary-article-menu gnus-summary-mode-map ""
2396         (cons "Article" innards))
2397
2398       (if (not (keymapp gnus-summary-article-menu))
2399           (easy-menu-define
2400             gnus-article-commands-menu gnus-article-mode-map ""
2401             (cons "Commands" innards))
2402         ;; in Emacs, don't share menu.
2403         (setq gnus-article-commands-menu
2404               (copy-keymap gnus-summary-article-menu))
2405         (define-key gnus-article-mode-map [menu-bar commands]
2406           (cons "Commands" gnus-article-commands-menu))))
2407
2408     (easy-menu-define
2409       gnus-summary-thread-menu gnus-summary-mode-map ""
2410       '("Threads"
2411         ["Find all messages in thread" gnus-summary-refer-thread t]
2412         ["Toggle threading" gnus-summary-toggle-threads t]
2413         ["Hide threads" gnus-summary-hide-all-threads t]
2414         ["Show threads" gnus-summary-show-all-threads t]
2415         ["Hide thread" gnus-summary-hide-thread t]
2416         ["Show thread" gnus-summary-show-thread t]
2417         ["Go to next thread" gnus-summary-next-thread t]
2418         ["Go to previous thread" gnus-summary-prev-thread t]
2419         ["Go down thread" gnus-summary-down-thread t]
2420         ["Go up thread" gnus-summary-up-thread t]
2421         ["Top of thread" gnus-summary-top-thread t]
2422         ["Mark thread as read" gnus-summary-kill-thread t]
2423         ["Lower thread score" gnus-summary-lower-thread t]
2424         ["Raise thread score" gnus-summary-raise-thread t]
2425         ["Rethread current" gnus-summary-rethread-current t]))
2426
2427     (easy-menu-define
2428       gnus-summary-post-menu gnus-summary-mode-map ""
2429       `("Post"
2430         ["Send a message (mail or news)" gnus-summary-post-news
2431          ,@(if (featurep 'xemacs) '(t)
2432              '(:help "Compose a new message (mail or news)"))]
2433         ["Followup" gnus-summary-followup
2434          ,@(if (featurep 'xemacs) '(t)
2435              '(:help "Post followup to this article"))]
2436         ["Followup and yank" gnus-summary-followup-with-original
2437          ,@(if (featurep 'xemacs) '(t)
2438              '(:help "Post followup to this article, quoting its contents"))]
2439         ["Supersede article" gnus-summary-supersede-article t]
2440         ["Cancel article" gnus-summary-cancel-article
2441          ,@(if (featurep 'xemacs) '(t)
2442              '(:help "Cancel an article you posted"))]
2443         ["Reply" gnus-summary-reply t]
2444         ["Reply and yank" gnus-summary-reply-with-original t]
2445         ["Wide reply" gnus-summary-wide-reply t]
2446         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2447          ,@(if (featurep 'xemacs) '(t)
2448              '(:help "Mail a reply, quoting this article"))]
2449         ["Very wide reply" gnus-summary-very-wide-reply t]
2450         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2451          ,@(if (featurep 'xemacs) '(t)
2452              '(:help "Mail a very wide reply, quoting this article"))]
2453         ["Mail forward" gnus-summary-mail-forward t]
2454         ["Post forward" gnus-summary-post-forward t]
2455         ["Digest and mail" gnus-uu-digest-mail-forward t]
2456         ["Digest and post" gnus-uu-digest-post-forward t]
2457         ["Resend message" gnus-summary-resend-message t]
2458         ["Resend message edit" gnus-summary-resend-message-edit t]
2459         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2460         ["Send a mail" gnus-summary-mail-other-window t]
2461         ["Create a local message" gnus-summary-news-other-window t]
2462         ["Uuencode and post" gnus-uu-post-news
2463          ,@(if (featurep 'xemacs) '(t)
2464              '(:help "Post a uuencoded article"))]
2465         ["Followup via news" gnus-summary-followup-to-mail t]
2466         ["Followup via news and yank"
2467          gnus-summary-followup-to-mail-with-original t]
2468         ;;("Draft"
2469         ;;["Send" gnus-summary-send-draft t]
2470         ;;["Send bounced" gnus-resend-bounced-mail t])
2471         ))
2472
2473     (cond
2474      ((not (keymapp gnus-summary-post-menu))
2475       (setq gnus-article-post-menu gnus-summary-post-menu))
2476      ((not gnus-article-post-menu)
2477       ;; Don't share post menu.
2478       (setq gnus-article-post-menu
2479             (copy-keymap gnus-summary-post-menu))))
2480     (define-key gnus-article-mode-map [menu-bar post]
2481       (cons "Post" gnus-article-post-menu))
2482
2483     (easy-menu-define
2484       gnus-summary-misc-menu gnus-summary-mode-map ""
2485       `("Gnus"
2486         ("Mark Read"
2487          ["Mark as read" gnus-summary-mark-as-read-forward t]
2488          ["Mark same subject and select"
2489           gnus-summary-kill-same-subject-and-select t]
2490          ["Mark same subject" gnus-summary-kill-same-subject t]
2491          ["Catchup" gnus-summary-catchup
2492           ,@(if (featurep 'xemacs) '(t)
2493               '(:help "Mark unread articles in this group as read"))]
2494          ["Catchup all" gnus-summary-catchup-all t]
2495          ["Catchup to here" gnus-summary-catchup-to-here t]
2496          ["Catchup from here" gnus-summary-catchup-from-here t]
2497          ["Catchup region" gnus-summary-mark-region-as-read
2498           (gnus-mark-active-p)]
2499          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2500         ("Mark Various"
2501          ["Tick" gnus-summary-tick-article-forward t]
2502          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2503          ["Remove marks" gnus-summary-clear-mark-forward t]
2504          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2505          ["Set bookmark" gnus-summary-set-bookmark t]
2506          ["Remove bookmark" gnus-summary-remove-bookmark t])
2507         ("Limit to"
2508          ["Marks..." gnus-summary-limit-to-marks t]
2509          ["Subject..." gnus-summary-limit-to-subject t]
2510          ["Author..." gnus-summary-limit-to-author t]
2511          ["Recipient..." gnus-summary-limit-to-recipient t]
2512          ["Age..." gnus-summary-limit-to-age t]
2513          ["Extra..." gnus-summary-limit-to-extra t]
2514          ["Score..." gnus-summary-limit-to-score t]
2515          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2516          ["Unread" gnus-summary-limit-to-unread t]
2517          ["Unseen" gnus-summary-limit-to-unseen t]
2518          ["Replied" gnus-summary-limit-to-replied t]
2519          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2520          ["Next articles" gnus-summary-limit-to-articles t]
2521          ["Pop limit" gnus-summary-pop-limit t]
2522          ["Show dormant" gnus-summary-limit-include-dormant t]
2523          ["Hide childless dormant"
2524           gnus-summary-limit-exclude-childless-dormant t]
2525          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2526          ["Hide marked" gnus-summary-limit-exclude-marks t]
2527          ["Show expunged" gnus-summary-limit-include-expunged t])
2528         ("Process Mark"
2529          ["Set mark" gnus-summary-mark-as-processable t]
2530          ["Remove mark" gnus-summary-unmark-as-processable t]
2531          ["Remove all marks" gnus-summary-unmark-all-processable t]
2532          ["Invert marks" gnus-uu-invert-processable t]
2533          ["Mark above" gnus-uu-mark-over t]
2534          ["Mark series" gnus-uu-mark-series t]
2535          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2536          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2537          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2538          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2539          ["Mark all" gnus-uu-mark-all t]
2540          ["Mark buffer" gnus-uu-mark-buffer t]
2541          ["Mark sparse" gnus-uu-mark-sparse t]
2542          ["Mark thread" gnus-uu-mark-thread t]
2543          ["Unmark thread" gnus-uu-unmark-thread t]
2544          ("Process Mark Sets"
2545           ["Kill" gnus-summary-kill-process-mark t]
2546           ["Yank" gnus-summary-yank-process-mark
2547            gnus-newsgroup-process-stack]
2548           ["Save" gnus-summary-save-process-mark t]
2549           ["Run command on marked..." gnus-summary-universal-argument t]))
2550         ("Scroll article"
2551          ["Page forward" gnus-summary-next-page
2552           ,@(if (featurep 'xemacs) '(t)
2553               '(:help "Show next page of article"))]
2554          ["Page backward" gnus-summary-prev-page
2555           ,@(if (featurep 'xemacs) '(t)
2556               '(:help "Show previous page of article"))]
2557          ["Line forward" gnus-summary-scroll-up t])
2558         ("Move"
2559          ["Next unread article" gnus-summary-next-unread-article t]
2560          ["Previous unread article" gnus-summary-prev-unread-article t]
2561          ["Next article" gnus-summary-next-article t]
2562          ["Previous article" gnus-summary-prev-article t]
2563          ["Next unread subject" gnus-summary-next-unread-subject t]
2564          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2565          ["Next article same subject" gnus-summary-next-same-subject t]
2566          ["Previous article same subject" gnus-summary-prev-same-subject t]
2567          ["First unread article" gnus-summary-first-unread-article t]
2568          ["Best unread article" gnus-summary-best-unread-article t]
2569          ["Go to subject number..." gnus-summary-goto-subject t]
2570          ["Go to article number..." gnus-summary-goto-article t]
2571          ["Go to the last article" gnus-summary-goto-last-article t]
2572          ["Pop article off history" gnus-summary-pop-article t])
2573         ("Sort"
2574          ["Sort by number" gnus-summary-sort-by-number t]
2575          ["Sort by author" gnus-summary-sort-by-author t]
2576          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2577          ["Sort by subject" gnus-summary-sort-by-subject t]
2578          ["Sort by date" gnus-summary-sort-by-date t]
2579          ["Sort by score" gnus-summary-sort-by-score t]
2580          ["Sort by lines" gnus-summary-sort-by-lines t]
2581          ["Sort by characters" gnus-summary-sort-by-chars t]
2582          ["Randomize" gnus-summary-sort-by-random t]
2583          ["Original sort" gnus-summary-sort-by-original t])
2584         ("Help"
2585          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2586          ["Describe group" gnus-summary-describe-group t]
2587          ["Fetch charter" gnus-group-fetch-charter
2588           ,@(if (featurep 'xemacs) nil
2589               '(:help "Display the charter of the current group"))]
2590          ["Fetch control message" gnus-group-fetch-control
2591           ,@(if (featurep 'xemacs) nil
2592               '(:help "Display the archived control message for the current group"))]
2593          ["Read manual" gnus-info-find-node t])
2594         ("Modes"
2595          ["Pick and read" gnus-pick-mode t]
2596          ["Binary" gnus-binary-mode t])
2597         ("Regeneration"
2598          ["Regenerate" gnus-summary-prepare t]
2599          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2600          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2601          ["Toggle threading" gnus-summary-toggle-threads t])
2602         ["See old articles" gnus-summary-insert-old-articles t]
2603         ["See new articles" gnus-summary-insert-new-articles t]
2604         ["Filter articles..." gnus-summary-execute-command t]
2605         ["Run command on articles..." gnus-summary-universal-argument t]
2606         ["Search articles forward..." gnus-summary-search-article-forward t]
2607         ["Search articles backward..." gnus-summary-search-article-backward t]
2608         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2609         ["Expand window" gnus-summary-expand-window t]
2610         ["Expire expirable articles" gnus-summary-expire-articles
2611          (gnus-check-backend-function
2612           'request-expire-articles gnus-newsgroup-name)]
2613         ["Edit local kill file" gnus-summary-edit-local-kill t]
2614         ["Edit main kill file" gnus-summary-edit-global-kill t]
2615         ["Edit group parameters" gnus-summary-edit-parameters t]
2616         ["Customize group parameters" gnus-summary-customize-parameters t]
2617         ["Send a bug report" gnus-bug t]
2618         ("Exit"
2619          ["Catchup and exit" gnus-summary-catchup-and-exit
2620           ,@(if (featurep 'xemacs) '(t)
2621               '(:help "Mark unread articles in this group as read, then exit"))]
2622          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2623          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2624          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2625          ["Exit group" gnus-summary-exit
2626           ,@(if (featurep 'xemacs) '(t)
2627               '(:help "Exit current group, return to group selection mode"))]
2628          ["Exit group without updating" gnus-summary-exit-no-update t]
2629          ["Exit and goto next group" gnus-summary-next-group t]
2630          ["Exit and goto prev group" gnus-summary-prev-group t]
2631          ["Reselect group" gnus-summary-reselect-current-group t]
2632          ["Rescan group" gnus-summary-rescan-group t]
2633          ["Update dribble" gnus-summary-save-newsrc t])))
2634
2635     (gnus-run-hooks 'gnus-summary-menu-hook)))
2636
2637 (defvar gnus-summary-tool-bar-map nil)
2638
2639 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2640 ;; affect _new_ message buffers.  We might add a function that walks thru all
2641 ;; summary-mode buffers and force the update.
2642 (defun gnus-summary-tool-bar-update (&optional symbol value)
2643   "Update summary mode toolbar.
2644 Setter function for custom variables."
2645   (setq-default gnus-summary-tool-bar-map nil)
2646   (when symbol
2647     ;; When used as ":set" function:
2648     (set-default symbol value))
2649   (when (gnus-buffer-live-p gnus-summary-buffer)
2650     (with-current-buffer gnus-summary-buffer
2651       (gnus-summary-make-tool-bar))))
2652
2653 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2654                                      'gnus-summary-tool-bar-gnome
2655                                    'gnus-summary-tool-bar-retro)
2656   "Specifies the Gnus summary tool bar.
2657
2658 It can be either a list or a symbol refering to a list.  See
2659 `gmm-tool-bar-from-list' for the format of the list.  The
2660 default key map is `gnus-summary-mode-map'.
2661
2662 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2663 `gnus-summary-tool-bar-retro'."
2664   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2665                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2666                  (repeat :tag "User defined list" gmm-tool-bar-item)
2667                  (symbol))
2668   :version "23.0" ;; No Gnus
2669   :initialize 'custom-initialize-default
2670   :set 'gnus-summary-tool-bar-update
2671   :group 'gnus-summary)
2672
2673 (defcustom gnus-summary-tool-bar-gnome
2674   '((gnus-summary-post-news "mail/compose" nil)
2675     (gnus-summary-insert-new-articles "mail/inbox" nil
2676                                       :visible (or (not gnus-agent)
2677                                                    gnus-plugged))
2678     (gnus-summary-reply-with-original "mail/reply")
2679     (gnus-summary-reply "mail/reply" nil :visible nil)
2680     (gnus-summary-followup-with-original "mail/reply-all")
2681     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2682     (gnus-summary-mail-forward "mail/forward")
2683     (gnus-summary-save-article "mail/save")
2684     (gnus-summary-search-article-forward "search" nil :visible nil)
2685     (gnus-summary-print-article "print")
2686     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2687     ;; Some new commands that may need more suitable icons:
2688     (gnus-summary-save-newsrc "save" nil :visible nil)
2689     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2690     (gnus-summary-prev-article "left-arrow")
2691     (gnus-summary-next-article "right-arrow")
2692     (gnus-summary-next-page "next-page")
2693     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2694     ;;
2695     ;; Maybe some sort-by-... could be added:
2696     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2697     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2698     (gnus-summary-mark-as-expirable
2699      "delete" nil
2700      :visible (gnus-check-backend-function 'request-expire-articles
2701                                            gnus-newsgroup-name))
2702     (gnus-summary-mark-as-spam
2703      "mail/spam" t
2704      :visible (and (fboundp 'spam-group-ham-contents-p)
2705                    (spam-group-ham-contents-p gnus-newsgroup-name))
2706      :help "Mark as spam")
2707     (gnus-summary-mark-as-read-forward
2708      "mail/not-spam" nil
2709      :visible (and (fboundp 'spam-group-spam-contents-p)
2710                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2711     ;;
2712     (gnus-summary-exit "exit")
2713     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2714     (gnus-info-find-node "help"))
2715   "List of functions for the summary tool bar (GNOME style).
2716
2717 See `gmm-tool-bar-from-list' for the format of the list."
2718   :type '(repeat gmm-tool-bar-item)
2719   :version "23.0" ;; No Gnus
2720   :initialize 'custom-initialize-default
2721   :set 'gnus-summary-tool-bar-update
2722   :group 'gnus-summary)
2723
2724 (defcustom gnus-summary-tool-bar-retro
2725   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2726     (gnus-summary-next-unread-article "gnus/next-ur")
2727     (gnus-summary-post-news "gnus/post")
2728     (gnus-summary-followup-with-original "gnus/fuwo")
2729     (gnus-summary-followup "gnus/followup")
2730     (gnus-summary-reply-with-original "gnus/reply-wo")
2731     (gnus-summary-reply "gnus/reply")
2732     (gnus-summary-caesar-message "gnus/rot13")
2733     (gnus-uu-decode-uu "gnus/uu-decode")
2734     (gnus-summary-save-article-file "gnus/save-aif")
2735     (gnus-summary-save-article "gnus/save-art")
2736     (gnus-uu-post-news "gnus/uu-post")
2737     (gnus-summary-catchup "gnus/catchup")
2738     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2739     (gnus-summary-exit "gnus/exit-summ")
2740     ;; Some new command that may need more suitable icons:
2741     (gnus-summary-print-article "gnus/print" nil :visible nil)
2742     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2743     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2744     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2745     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2746     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2747     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2748     ;;
2749     (gnus-info-find-node "gnus/help" nil :visible nil))
2750   "List of functions for the summary tool bar (retro look).
2751
2752 See `gmm-tool-bar-from-list' for the format of the list."
2753   :type '(repeat gmm-tool-bar-item)
2754   :version "23.0" ;; No Gnus
2755   :initialize 'custom-initialize-default
2756   :set 'gnus-summary-tool-bar-update
2757   :group 'gnus-summary)
2758
2759 (defcustom gnus-summary-tool-bar-zap-list t
2760   "List of icon items from the global tool bar.
2761 These items are not displayed in the Gnus summary mode tool bar.
2762
2763 See `gmm-tool-bar-from-list' for the format of the list."
2764   :type 'gmm-tool-bar-zap-list
2765   :version "23.0" ;; No Gnus
2766   :initialize 'custom-initialize-default
2767   :set 'gnus-summary-tool-bar-update
2768   :group 'gnus-summary)
2769
2770 (defvar image-load-path)
2771
2772 (defun gnus-summary-make-tool-bar (&optional force)
2773   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2774 When FORCE, rebuild the tool bar."
2775   (when (and (not (featurep 'xemacs))
2776              (boundp 'tool-bar-mode)
2777              tool-bar-mode
2778              (or (not gnus-summary-tool-bar-map) force))
2779     (let* ((load-path
2780             (gmm-image-load-path-for-library "gnus"
2781                                              "mail/save.xpm"
2782                                              nil t))
2783            (image-load-path (cons (car load-path)
2784                                   (when (boundp 'image-load-path)
2785                                     image-load-path)))
2786            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2787                                         gnus-summary-tool-bar-zap-list
2788                                         'gnus-summary-mode-map)))
2789       (when map
2790         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2791         ;; uses it's value.
2792         (setq gnus-summary-tool-bar-map map))))
2793   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2794
2795 (defun gnus-score-set-default (var value)
2796   "A version of set that updates the GNU Emacs menu-bar."
2797   (set var value)
2798   ;; It is the message that forces the active status to be updated.
2799   (message ""))
2800
2801 (defun gnus-make-score-map (type)
2802   "Make a summary score map of type TYPE."
2803   (if t
2804       nil
2805     (let ((headers '(("author" "from" string)
2806                      ("subject" "subject" string)
2807                      ("article body" "body" string)
2808                      ("article head" "head" string)
2809                      ("xref" "xref" string)
2810                      ("extra header" "extra" string)
2811                      ("lines" "lines" number)
2812                      ("followups to author" "followup" string)))
2813           (types '((number ("less than" <)
2814                            ("greater than" >)
2815                            ("equal" =))
2816                    (string ("substring" s)
2817                            ("exact string" e)
2818                            ("fuzzy string" f)
2819                            ("regexp" r))))
2820           (perms '(("temporary" (current-time-string))
2821                    ("permanent" nil)
2822                    ("immediate" now)))
2823           header)
2824       (list
2825        (apply
2826         'nconc
2827         (list
2828          (if (eq type 'lower)
2829              "Lower score"
2830            "Increase score"))
2831         (let (outh)
2832           (while headers
2833             (setq header (car headers))
2834             (setq outh
2835                   (cons
2836                    (apply
2837                     'nconc
2838                     (list (car header))
2839                     (let ((ts (cdr (assoc (nth 2 header) types)))
2840                           outt)
2841                       (while ts
2842                         (setq outt
2843                               (cons
2844                                (apply
2845                                 'nconc
2846                                 (list (caar ts))
2847                                 (let ((ps perms)
2848                                       outp)
2849                                   (while ps
2850                                     (setq outp
2851                                           (cons
2852                                            (vector
2853                                             (caar ps)
2854                                             (list
2855                                              'gnus-summary-score-entry
2856                                              (nth 1 header)
2857                                              (if (or (string= (nth 1 header)
2858                                                               "head")
2859                                                      (string= (nth 1 header)
2860                                                               "body"))
2861                                                  ""
2862                                                (list 'gnus-summary-header
2863                                                      (nth 1 header)))
2864                                              (list 'quote (nth 1 (car ts)))
2865                                              (list 'gnus-score-delta-default
2866                                                    nil)
2867                                              (nth 1 (car ps))
2868                                              t)
2869                                             t)
2870                                            outp))
2871                                     (setq ps (cdr ps)))
2872                                   (list (nreverse outp))))
2873                                outt))
2874                         (setq ts (cdr ts)))
2875                       (list (nreverse outt))))
2876                    outh))
2877             (setq headers (cdr headers)))
2878           (list (nreverse outh))))))))
2879
2880 \f
2881
2882 (defun gnus-summary-mode (&optional group)
2883   "Major mode for reading articles.
2884
2885 All normal editing commands are switched off.
2886 \\<gnus-summary-mode-map>
2887 Each line in this buffer represents one article.  To read an
2888 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2889 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2890 respectively.
2891
2892 You can also post articles and send mail from this buffer.  To
2893 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2894 of an article, type `\\[gnus-summary-reply]'.
2895
2896 There are approx. one gazillion commands you can execute in this
2897 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2898
2899 The following commands are available:
2900
2901 \\{gnus-summary-mode-map}"
2902   (interactive)
2903   (kill-all-local-variables)
2904   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2905     (gnus-summary-make-local-variables))
2906   (gnus-summary-make-local-variables)
2907   (setq gnus-newsgroup-name group)
2908   (when (gnus-visual-p 'summary-menu 'menu)
2909     (gnus-summary-make-menu-bar)
2910     (gnus-summary-make-tool-bar))
2911   (gnus-make-thread-indent-array)
2912   (gnus-simplify-mode-line)
2913   (setq major-mode 'gnus-summary-mode)
2914   (setq mode-name "Summary")
2915   (make-local-variable 'minor-mode-alist)
2916   (use-local-map gnus-summary-mode-map)
2917   (buffer-disable-undo)
2918   (setq buffer-read-only t              ;Disable modification
2919         show-trailing-whitespace nil)
2920   (setq truncate-lines t)
2921   (setq selective-display t)
2922   (setq selective-display-ellipses t)   ;Display `...'
2923   (gnus-summary-set-display-table)
2924   (gnus-set-default-directory)
2925   (make-local-variable 'gnus-summary-line-format)
2926   (make-local-variable 'gnus-summary-line-format-spec)
2927   (make-local-variable 'gnus-summary-dummy-line-format)
2928   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2929   (make-local-variable 'gnus-summary-mark-positions)
2930   (gnus-make-local-hook 'pre-command-hook)
2931   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2932   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
2933   (turn-on-gnus-mailing-list-mode)
2934   (mm-enable-multibyte)
2935   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2936   (gnus-update-summary-mark-positions))
2937
2938 (defun gnus-summary-make-local-variables ()
2939   "Make all the local summary buffer variables."
2940   (let (global)
2941     (dolist (local gnus-summary-local-variables)
2942       (if (consp local)
2943           (progn
2944             (if (eq (cdr local) 'global)
2945                 ;; Copy the global value of the variable.
2946                 (setq global (symbol-value (car local)))
2947               ;; Use the value from the list.
2948               (setq global (eval (cdr local))))
2949             (set (make-local-variable (car local)) global))
2950         ;; Simple nil-valued local variable.
2951         (set (make-local-variable local) nil)))))
2952
2953 (defun gnus-summary-clear-local-variables ()
2954   (let ((locals gnus-summary-local-variables))
2955     (while locals
2956       (if (consp (car locals))
2957           (and (symbolp (caar locals))
2958                (set (caar locals) nil))
2959         (and (symbolp (car locals))
2960              (set (car locals) nil)))
2961       (setq locals (cdr locals)))))
2962
2963 ;; Summary data functions.
2964
2965 (defmacro gnus-data-number (data)
2966   `(car ,data))
2967
2968 (defmacro gnus-data-set-number (data number)
2969   `(setcar ,data ,number))
2970
2971 (defmacro gnus-data-mark (data)
2972   `(nth 1 ,data))
2973
2974 (defmacro gnus-data-set-mark (data mark)
2975   `(setcar (nthcdr 1 ,data) ,mark))
2976
2977 (defmacro gnus-data-pos (data)
2978   `(nth 2 ,data))
2979
2980 (defmacro gnus-data-set-pos (data pos)
2981   `(setcar (nthcdr 2 ,data) ,pos))
2982
2983 (defmacro gnus-data-header (data)
2984   `(nth 3 ,data))
2985
2986 (defmacro gnus-data-set-header (data header)
2987   `(setf (nth 3 ,data) ,header))
2988
2989 (defmacro gnus-data-level (data)
2990   `(nth 4 ,data))
2991
2992 (defmacro gnus-data-unread-p (data)
2993   `(= (nth 1 ,data) gnus-unread-mark))
2994
2995 (defmacro gnus-data-read-p (data)
2996   `(/= (nth 1 ,data) gnus-unread-mark))
2997
2998 (defmacro gnus-data-pseudo-p (data)
2999   `(consp (nth 3 ,data)))
3000
3001 (defmacro gnus-data-find (number)
3002   `(assq ,number gnus-newsgroup-data))
3003
3004 (defmacro gnus-data-find-list (number &optional data)
3005   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3006      (memq (assq ,number bdata)
3007            bdata)))
3008
3009 (defmacro gnus-data-make (number mark pos header level)
3010   `(list ,number ,mark ,pos ,header ,level))
3011
3012 (defun gnus-data-enter (after-article number mark pos header level offset)
3013   (let ((data (gnus-data-find-list after-article)))
3014     (unless data
3015       (error "No such article: %d" after-article))
3016     (setcdr data (cons (gnus-data-make number mark pos header level)
3017                        (cdr data)))
3018     (setq gnus-newsgroup-data-reverse nil)
3019     (gnus-data-update-list (cddr data) offset)))
3020
3021 (defun gnus-data-enter-list (after-article list &optional offset)
3022   (when list
3023     (let ((data (and after-article (gnus-data-find-list after-article)))
3024           (ilist list))
3025       (if (not (or data
3026                    after-article))
3027           (let ((odata gnus-newsgroup-data))
3028             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3029             (when offset
3030               (gnus-data-update-list odata offset)))
3031         ;; Find the last element in the list to be spliced into the main
3032         ;; list.
3033         (setq list (last list))
3034         (if (not data)
3035             (progn
3036               (setcdr list gnus-newsgroup-data)
3037               (setq gnus-newsgroup-data ilist)
3038               (when offset
3039                 (gnus-data-update-list (cdr list) offset)))
3040           (setcdr list (cdr data))
3041           (setcdr data ilist)
3042           (when offset
3043             (gnus-data-update-list (cdr list) offset))))
3044       (setq gnus-newsgroup-data-reverse nil))))
3045
3046 (defun gnus-data-remove (article &optional offset)
3047   (let ((data gnus-newsgroup-data))
3048     (if (= (gnus-data-number (car data)) article)
3049         (progn
3050           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3051                 gnus-newsgroup-data-reverse nil)
3052           (when offset
3053             (gnus-data-update-list gnus-newsgroup-data offset)))
3054       (while (cdr data)
3055         (when (= (gnus-data-number (cadr data)) article)
3056           (setcdr data (cddr data))
3057           (when offset
3058             (gnus-data-update-list (cdr data) offset))
3059           (setq data nil
3060                 gnus-newsgroup-data-reverse nil))
3061         (setq data (cdr data))))))
3062
3063 (defmacro gnus-data-list (backward)
3064   `(if ,backward
3065        (or gnus-newsgroup-data-reverse
3066            (setq gnus-newsgroup-data-reverse
3067                  (reverse gnus-newsgroup-data)))
3068      gnus-newsgroup-data))
3069
3070 (defun gnus-data-update-list (data offset)
3071   "Add OFFSET to the POS of all data entries in DATA."
3072   (setq gnus-newsgroup-data-reverse nil)
3073   (while data
3074     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3075     (setq data (cdr data))))
3076
3077 (defun gnus-summary-article-pseudo-p (article)
3078   "Say whether this article is a pseudo article or not."
3079   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3080
3081 (defmacro gnus-summary-article-sparse-p (article)
3082   "Say whether this article is a sparse article or not."
3083   `(memq ,article gnus-newsgroup-sparse))
3084
3085 (defmacro gnus-summary-article-ancient-p (article)
3086   "Say whether this article is a sparse article or not."
3087   `(memq ,article gnus-newsgroup-ancient))
3088
3089 (defun gnus-article-parent-p (number)
3090   "Say whether this article is a parent or not."
3091   (let ((data (gnus-data-find-list number)))
3092     (and (cdr data)              ; There has to be an article after...
3093          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3094             (gnus-data-level (nth 1 data))))))
3095
3096 (defun gnus-article-children (number)
3097   "Return a list of all children to NUMBER."
3098   (let* ((data (gnus-data-find-list number))
3099          (level (gnus-data-level (car data)))
3100          children)
3101     (setq data (cdr data))
3102     (while (and data
3103                 (= (gnus-data-level (car data)) (1+ level)))
3104       (push (gnus-data-number (car data)) children)
3105       (setq data (cdr data)))
3106     children))
3107
3108 (defmacro gnus-summary-skip-intangible ()
3109   "If the current article is intangible, then jump to a different article."
3110   '(let ((to (get-text-property (point) 'gnus-intangible)))
3111      (and to (gnus-summary-goto-subject to))))
3112
3113 (defmacro gnus-summary-article-intangible-p ()
3114   "Say whether this article is intangible or not."
3115   '(get-text-property (point) 'gnus-intangible))
3116
3117 (defun gnus-article-read-p (article)
3118   "Say whether ARTICLE is read or not."
3119   (not (or (memq article gnus-newsgroup-marked)
3120            (memq article gnus-newsgroup-spam-marked)
3121            (memq article gnus-newsgroup-unreads)
3122            (memq article gnus-newsgroup-unselected)
3123            (memq article gnus-newsgroup-dormant))))
3124
3125 ;; Some summary mode macros.
3126
3127 (defmacro gnus-summary-article-number ()
3128   "The article number of the article on the current line.
3129 If there isn't an article number here, then we return the current
3130 article number."
3131   '(progn
3132      (gnus-summary-skip-intangible)
3133      (or (get-text-property (point) 'gnus-number)
3134          (gnus-summary-last-subject))))
3135
3136 (defmacro gnus-summary-article-header (&optional number)
3137   "Return the header of article NUMBER."
3138   `(gnus-data-header (gnus-data-find
3139                       ,(or number '(gnus-summary-article-number)))))
3140
3141 (defmacro gnus-summary-thread-level (&optional number)
3142   "Return the level of thread that starts with article NUMBER."
3143   `(if (and (eq gnus-summary-make-false-root 'dummy)
3144             (get-text-property (point) 'gnus-intangible))
3145        0
3146      (gnus-data-level (gnus-data-find
3147                        ,(or number '(gnus-summary-article-number))))))
3148
3149 (defmacro gnus-summary-article-mark (&optional number)
3150   "Return the mark of article NUMBER."
3151   `(gnus-data-mark (gnus-data-find
3152                     ,(or number '(gnus-summary-article-number)))))
3153
3154 (defmacro gnus-summary-article-pos (&optional number)
3155   "Return the position of the line of article NUMBER."
3156   `(gnus-data-pos (gnus-data-find
3157                    ,(or number '(gnus-summary-article-number)))))
3158
3159 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3160 (defmacro gnus-summary-article-subject (&optional number)
3161   "Return current subject string or nil if nothing."
3162   `(let ((headers
3163           ,(if number
3164                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3165              '(gnus-data-header (assq (gnus-summary-article-number)
3166                                       gnus-newsgroup-data)))))
3167      (and headers
3168           (vectorp headers)
3169           (mail-header-subject headers))))
3170
3171 (defmacro gnus-summary-article-score (&optional number)
3172   "Return current article score."
3173   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3174                   gnus-newsgroup-scored))
3175        gnus-summary-default-score 0))
3176
3177 (defun gnus-summary-article-children (&optional number)
3178   "Return a list of article numbers that are children of article NUMBER."
3179   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3180          (level (gnus-data-level (car data)))
3181          l children)
3182     (while (and (setq data (cdr data))
3183                 (> (setq l (gnus-data-level (car data))) level))
3184       (and (= (1+ level) l)
3185            (push (gnus-data-number (car data))
3186                  children)))
3187     (nreverse children)))
3188
3189 (defun gnus-summary-article-parent (&optional number)
3190   "Return the article number of the parent of article NUMBER."
3191   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3192                                     (gnus-data-list t)))
3193          (level (gnus-data-level (car data))))
3194     (if (zerop level)
3195         ()                              ; This is a root.
3196       ;; We search until we find an article with a level less than
3197       ;; this one.  That function has to be the parent.
3198       (while (and (setq data (cdr data))
3199                   (not (< (gnus-data-level (car data)) level))))
3200       (and data (gnus-data-number (car data))))))
3201
3202 (defun gnus-unread-mark-p (mark)
3203   "Say whether MARK is the unread mark."
3204   (= mark gnus-unread-mark))
3205
3206 (defun gnus-read-mark-p (mark)
3207   "Say whether MARK is one of the marks that mark as read.
3208 This is all marks except unread, ticked, dormant, and expirable."
3209   (not (or (= mark gnus-unread-mark)
3210            (= mark gnus-ticked-mark)
3211            (= mark gnus-spam-mark)
3212            (= mark gnus-dormant-mark)
3213            (= mark gnus-expirable-mark))))
3214
3215 (defmacro gnus-article-mark (number)
3216   "Return the MARK of article NUMBER.
3217 This macro should only be used when computing the mark the \"first\"
3218 time; i.e., when generating the summary lines.  After that,
3219 `gnus-summary-article-mark' should be used to examine the
3220 marks of articles."
3221   `(cond
3222     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3223     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3224     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3225     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3226     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3227     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3228     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3229     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3230            gnus-ancient-mark))))
3231
3232 ;; Saving hidden threads.
3233
3234 (defmacro gnus-save-hidden-threads (&rest forms)
3235   "Save hidden threads, eval FORMS, and restore the hidden threads."
3236   (let ((config (make-symbol "config")))
3237     `(let ((,config (gnus-hidden-threads-configuration)))
3238        (unwind-protect
3239            (save-excursion
3240              ,@forms)
3241          (gnus-restore-hidden-threads-configuration ,config)))))
3242 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3243 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3244
3245 (defun gnus-data-compute-positions ()
3246   "Compute the positions of all articles."
3247   (setq gnus-newsgroup-data-reverse nil)
3248   (let ((data gnus-newsgroup-data))
3249     (save-excursion
3250       (gnus-save-hidden-threads
3251         (gnus-summary-show-all-threads)
3252         (goto-char (point-min))
3253         (while data
3254           (while (get-text-property (point) 'gnus-intangible)
3255             (forward-line 1))
3256           (gnus-data-set-pos (car data) (+ (point) 3))
3257           (setq data (cdr data))
3258           (forward-line 1))))))
3259
3260 (defun gnus-hidden-threads-configuration ()
3261   "Return the current hidden threads configuration."
3262   (save-excursion
3263     (let (config)
3264       (goto-char (point-min))
3265       (while (search-forward "\r" nil t)
3266         (push (1- (point)) config))
3267       config)))
3268
3269 (defun gnus-restore-hidden-threads-configuration (config)
3270   "Restore hidden threads configuration from CONFIG."
3271   (save-excursion
3272     (let (point buffer-read-only)
3273       (while (setq point (pop config))
3274         (when (and (< point (point-max))
3275                    (goto-char point)
3276                    (eq (char-after) ?\n))
3277           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3278
3279 ;; Various summary mode internalish functions.
3280
3281 (defun gnus-mouse-pick-article (e)
3282   (interactive "e")
3283   (mouse-set-point e)
3284   (gnus-summary-next-page nil t))
3285
3286 (defun gnus-summary-set-display-table ()
3287   "Change the display table.
3288 Odd characters have a tendency to mess
3289 up nicely formatted displays - we make all possible glyphs
3290 display only a single character."
3291
3292   ;; We start from the standard display table, if any.
3293   (let ((table (or (copy-sequence standard-display-table)
3294                    (make-display-table)))
3295         (i 32))
3296     ;; Nix out all the control chars...
3297     (while (>= (setq i (1- i)) 0)
3298       (aset table i [??]))
3299    ;; ... but not newline and cr, of course.  (cr is necessary for the
3300     ;; selective display).
3301     (aset table ?\n nil)
3302     (aset table ?\r nil)
3303     ;; We keep TAB as well.
3304     (aset table ?\t nil)
3305     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3306     ;; Emacs 23 (unicode), that are not set already.
3307     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3308                  160
3309                256)))
3310       (while (>= (setq i (1- i)) 127)
3311         ;; Only modify if the entry is nil.
3312         (unless (aref table i)
3313           (aset table i [??]))))
3314     (setq buffer-display-table table)))
3315
3316 (defun gnus-summary-set-article-display-arrow (pos)
3317   "Update the overlay arrow to point to line at position POS."
3318   (when (and gnus-summary-display-arrow
3319              (boundp 'overlay-arrow-position)
3320              (boundp 'overlay-arrow-string))
3321     (save-excursion
3322       (goto-char pos)
3323       (beginning-of-line)
3324       (unless overlay-arrow-position
3325         (setq overlay-arrow-position (make-marker)))
3326       (setq overlay-arrow-string "=>"
3327             overlay-arrow-position (set-marker overlay-arrow-position
3328                                                (point)
3329                                                (current-buffer))))))
3330
3331 (defun gnus-summary-setup-buffer (group)
3332   "Initialize summary buffer."
3333   (let ((buffer (gnus-summary-buffer-name group))
3334         (dead-name (concat "*Dead Summary "
3335                            (gnus-group-decoded-name group) "*")))
3336     ;; If a dead summary buffer exists, we kill it.
3337     (when (gnus-buffer-live-p dead-name)
3338       (gnus-kill-buffer dead-name))
3339     (if (get-buffer buffer)
3340         (progn
3341           (set-buffer buffer)
3342           (setq gnus-summary-buffer (current-buffer))
3343           (not gnus-newsgroup-prepared))
3344       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3345       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3346       (gnus-summary-mode group)
3347       (when gnus-carpal
3348         (gnus-carpal-setup-buffer 'summary))
3349       (unless gnus-single-article-buffer
3350         (make-local-variable 'gnus-article-buffer)
3351         (make-local-variable 'gnus-article-current)
3352         (make-local-variable 'gnus-original-article-buffer))
3353       (setq gnus-newsgroup-name group)
3354       ;; Set any local variables in the group parameters.
3355       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3356       t)))
3357
3358 (defun gnus-set-global-variables ()
3359   "Set the global equivalents of the buffer-local variables.
3360 They are set to the latest values they had.  These reflect the summary
3361 buffer that was in action when the last article was fetched."
3362   (when (eq major-mode 'gnus-summary-mode)
3363     (setq gnus-summary-buffer (current-buffer))
3364     (let ((name gnus-newsgroup-name)
3365           (marked gnus-newsgroup-marked)
3366           (spam gnus-newsgroup-spam-marked)
3367           (unread gnus-newsgroup-unreads)
3368           (headers gnus-current-headers)
3369           (data gnus-newsgroup-data)
3370           (summary gnus-summary-buffer)
3371           (article-buffer gnus-article-buffer)
3372           (original gnus-original-article-buffer)
3373           (gac gnus-article-current)
3374           (reffed gnus-reffed-article-number)
3375           (score-file gnus-current-score-file)
3376           (default-charset gnus-newsgroup-charset)
3377           vlist)
3378       (let ((locals gnus-newsgroup-variables))
3379         (while locals
3380           (if (consp (car locals))
3381               (push (eval (caar locals)) vlist)
3382             (push (eval (car locals)) vlist))
3383           (setq locals (cdr locals)))
3384         (setq vlist (nreverse vlist)))
3385       (with-current-buffer gnus-group-buffer
3386         (setq gnus-newsgroup-name name
3387               gnus-newsgroup-marked marked
3388               gnus-newsgroup-spam-marked spam
3389               gnus-newsgroup-unreads unread
3390               gnus-current-headers headers
3391               gnus-newsgroup-data data
3392               gnus-article-current gac
3393               gnus-summary-buffer summary
3394               gnus-article-buffer article-buffer
3395               gnus-original-article-buffer original
3396               gnus-reffed-article-number reffed
3397               gnus-current-score-file score-file
3398               gnus-newsgroup-charset default-charset)
3399         (let ((locals gnus-newsgroup-variables))
3400           (while locals
3401             (if (consp (car locals))
3402                 (set (caar locals) (pop vlist))
3403               (set (car locals) (pop vlist)))
3404             (setq locals (cdr locals))))
3405         ;; The article buffer also has local variables.
3406         (when (gnus-buffer-live-p gnus-article-buffer)
3407           (set-buffer gnus-article-buffer)
3408           (setq gnus-summary-buffer summary))))))
3409
3410 (defun gnus-summary-article-unread-p (article)
3411   "Say whether ARTICLE is unread or not."
3412   (memq article gnus-newsgroup-unreads))
3413
3414 (defun gnus-summary-first-article-p (&optional article)
3415   "Return whether ARTICLE is the first article in the buffer."
3416   (if (not (setq article (or article (gnus-summary-article-number))))
3417       nil
3418     (eq article (caar gnus-newsgroup-data))))
3419
3420 (defun gnus-summary-last-article-p (&optional article)
3421   "Return whether ARTICLE is the last article in the buffer."
3422   (if (not (setq article (or article (gnus-summary-article-number))))
3423       ;; All non-existent numbers are the last article.  :-)
3424       t
3425     (not (cdr (gnus-data-find-list article)))))
3426
3427 (defun gnus-make-thread-indent-array ()
3428   (let ((n 200))
3429     (unless (and gnus-thread-indent-array
3430                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3431       (setq gnus-thread-indent-array (make-vector 201 "")
3432             gnus-thread-indent-array-level gnus-thread-indent-level)
3433       (while (>= n 0)
3434         (aset gnus-thread-indent-array n
3435               (make-string (* n gnus-thread-indent-level) ? ))
3436         (setq n (1- n))))))
3437
3438 (defun gnus-update-summary-mark-positions ()
3439   "Compute where the summary marks are to go."
3440   (save-excursion
3441     (when (gnus-buffer-exists-p gnus-summary-buffer)
3442       (set-buffer gnus-summary-buffer))
3443     (let ((spec gnus-summary-line-format-spec)
3444           pos)
3445       (save-excursion
3446         (gnus-set-work-buffer)
3447         (let ((gnus-tmp-unread ?Z)
3448               (gnus-replied-mark ?Z)
3449               (gnus-score-below-mark ?Z)
3450               (gnus-score-over-mark ?Z)
3451               (gnus-undownloaded-mark ?Z)
3452               (gnus-summary-line-format-spec spec)
3453               (gnus-newsgroup-downloadable '(0))
3454               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3455               case-fold-search ignores)
3456           ;; Here, all marks are bound to Z.
3457           (gnus-summary-insert-line header
3458                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3459           (goto-char (point-min))
3460           ;; Memorize the positions of the same characters as dummy marks.
3461           (while (re-search-forward "[A-D]" nil t)
3462             (push (point) ignores))
3463           (erase-buffer)
3464           ;; We use A-D as dummy marks in order to know column positions
3465           ;; where marks should be inserted.
3466           (setq gnus-tmp-unread ?A
3467                 gnus-replied-mark ?B
3468                 gnus-score-below-mark ?C
3469                 gnus-score-over-mark ?C
3470                 gnus-undownloaded-mark ?D)
3471           (gnus-summary-insert-line header
3472                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3473           ;; Ignore characters which aren't dummy marks.
3474           (dolist (p ignores)
3475             (delete-region (goto-char (1- p)) p)
3476             (insert ?Z))
3477           (goto-char (point-min))
3478           (setq pos (list (cons 'unread
3479                                 (and (search-forward "A" nil t)
3480                                      (- (point) (point-min) 1)))))
3481           (goto-char (point-min))
3482           (push (cons 'replied (and (search-forward "B" nil t)
3483                                     (- (point) (point-min) 1)))
3484                 pos)
3485           (goto-char (point-min))
3486           (push (cons 'score (and (search-forward "C" nil t)
3487                                   (- (point) (point-min) 1)))
3488                 pos)
3489           (goto-char (point-min))
3490           (push (cons 'download (and (search-forward "D" nil t)
3491                                      (- (point) (point-min) 1)))
3492                 pos)))
3493       (setq gnus-summary-mark-positions pos))))
3494
3495 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3496   "Insert a dummy root in the summary buffer."
3497   (beginning-of-line)
3498   (gnus-add-text-properties
3499    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3500    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3501
3502 (defun gnus-summary-extract-address-component (from)
3503   (or (car (funcall gnus-extract-address-components from))
3504       from))
3505
3506 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3507   (let ((mail-parse-charset gnus-newsgroup-charset)
3508         ; Is it really necessary to do this next part for each summary line?
3509         ; Luckily, doesn't seem to slow things down much.
3510         (mail-parse-ignored-charsets
3511          (with-current-buffer gnus-summary-buffer
3512            gnus-newsgroup-ignored-charsets)))
3513     (or
3514      (and gnus-ignored-from-addresses
3515           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3516           (let ((extra-headers (mail-header-extra header))
3517                 to
3518                 newsgroups)
3519             (cond
3520              ((setq to (cdr (assq 'To extra-headers)))
3521               (concat gnus-summary-to-prefix
3522                       (inline
3523                         (gnus-summary-extract-address-component
3524                          (funcall gnus-decode-encoded-word-function to)))))
3525              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3526               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3527      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3528
3529 (defun gnus-summary-insert-line (gnus-tmp-header
3530                                  gnus-tmp-level gnus-tmp-current
3531                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3532                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3533                                  &optional gnus-tmp-dummy gnus-tmp-score
3534                                  gnus-tmp-process)
3535   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3536          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3537          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3538          (gnus-tmp-score-char
3539           (if (or (null gnus-summary-default-score)
3540                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3541                       gnus-summary-zcore-fuzz))
3542               ?                         ;Whitespace
3543             (if (< gnus-tmp-score gnus-summary-default-score)
3544                 gnus-score-below-mark gnus-score-over-mark)))
3545          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3546          (gnus-tmp-replied
3547           (cond (gnus-tmp-process gnus-process-mark)
3548                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3549                  gnus-cached-mark)
3550                 (gnus-tmp-replied gnus-replied-mark)
3551                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3552                  gnus-forwarded-mark)
3553                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3554                  gnus-saved-mark)
3555                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3556                  gnus-recent-mark)
3557                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3558                  gnus-unseen-mark)
3559                 (t gnus-no-mark)))
3560          (gnus-tmp-downloaded
3561           (cond (undownloaded
3562                  gnus-undownloaded-mark)
3563                 (gnus-newsgroup-agentized
3564                  gnus-downloaded-mark)
3565                 (t
3566                  gnus-no-mark)))
3567          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3568          (gnus-tmp-name
3569           (cond
3570            ((string-match "<[^>]+> *$" gnus-tmp-from)
3571             (let ((beg (match-beginning 0)))
3572               (or (and (string-match "^\".+\"" gnus-tmp-from)
3573                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3574                   (substring gnus-tmp-from 0 beg))))
3575            ((string-match "(.+)" gnus-tmp-from)
3576             (substring gnus-tmp-from
3577                        (1+ (match-beginning 0)) (1- (match-end 0))))
3578            (t gnus-tmp-from)))
3579          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3580          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3581          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3582          (buffer-read-only nil))
3583     (when (string= gnus-tmp-name "")
3584       (setq gnus-tmp-name gnus-tmp-from))
3585     (unless (numberp gnus-tmp-lines)
3586       (setq gnus-tmp-lines -1))
3587     (if (= gnus-tmp-lines -1)
3588         (setq gnus-tmp-lines "?")
3589       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3590       (gnus-put-text-property
3591      (point)
3592      (progn (eval gnus-summary-line-format-spec) (point))
3593        'gnus-number gnus-tmp-number)
3594     (when (gnus-visual-p 'summary-highlight 'highlight)
3595       (forward-line -1)
3596       (gnus-run-hooks 'gnus-summary-update-hook)
3597       (forward-line 1))))
3598
3599 (defun gnus-summary-update-line (&optional dont-update)
3600   "Update summary line after change."
3601   (when (and gnus-summary-default-score
3602              (not gnus-summary-inhibit-highlight))
3603     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3604            (article (gnus-summary-article-number))
3605            (score (gnus-summary-article-score article)))
3606       (unless dont-update
3607         (if (and gnus-summary-mark-below
3608                  (< (gnus-summary-article-score)
3609                     gnus-summary-mark-below))
3610             ;; This article has a low score, so we mark it as read.
3611             (when (memq article gnus-newsgroup-unreads)
3612               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3613           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3614             ;; This article was previously marked as read on account
3615             ;; of a low score, but now it has risen, so we mark it as
3616             ;; unread.
3617             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3618         (gnus-summary-update-mark
3619          (if (or (null gnus-summary-default-score)
3620                  (<= (abs (- score gnus-summary-default-score))
3621                      gnus-summary-zcore-fuzz))
3622              ?                          ;Whitespace
3623            (if (< score gnus-summary-default-score)
3624                gnus-score-below-mark gnus-score-over-mark))
3625          'score))
3626       ;; Do visual highlighting.
3627       (when (gnus-visual-p 'summary-highlight 'highlight)
3628         (gnus-run-hooks 'gnus-summary-update-hook)))))
3629
3630 (defvar gnus-tmp-new-adopts nil)
3631
3632 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3633   "Return the number of articles in THREAD.
3634 This may be 0 in some cases -- if none of the articles in
3635 the thread are to be displayed."
3636   (let* ((number
3637          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3638           (cond
3639            ((not (listp thread))
3640             1)
3641            ((and (consp thread) (cdr thread))
3642             (apply
3643              '+ 1 (mapcar
3644                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3645            ((null thread)
3646             1)
3647            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3648             1)
3649            (t 0))))
3650     (when (and level (zerop level) gnus-tmp-new-adopts)
3651       (incf number
3652             (apply '+ (mapcar
3653                        'gnus-summary-number-of-articles-in-thread
3654                        gnus-tmp-new-adopts))))
3655     (if char
3656         (if (> number 1) gnus-not-empty-thread-mark
3657           gnus-empty-thread-mark)
3658       number)))
3659
3660 (defsubst gnus-summary-line-message-size (head)
3661   "Return pretty-printed version of message size.
3662 This function is intended to be used in
3663 `gnus-summary-line-format-alist'."
3664   (let ((c (or (mail-header-chars head) -1)))
3665     (cond ((< c 0) "n/a")               ; chars not available
3666           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3667           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3668           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3669           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3670
3671
3672 (defun gnus-summary-set-local-parameters (group)
3673   "Go through the local params of GROUP and set all variable specs in that list."
3674   (let ((vars '(quit-config)))          ; Ignore quit-config.
3675     (dolist (elem (gnus-group-find-parameter group))
3676       (and (consp elem)                 ; Has to be a cons.
3677            (consp (cdr elem))           ; The cdr has to be a list.
3678            (symbolp (car elem))         ; Has to be a symbol in there.
3679            (not (memq (car elem) vars))
3680            (ignore-errors               ; So we set it.
3681              (push (car elem) vars)
3682              (make-local-variable (car elem))
3683              (set (car elem) (eval (nth 1 elem))))))))
3684
3685 (defun gnus-summary-read-group (group &optional show-all no-article
3686                                       kill-buffer no-display backward
3687                                       select-articles)
3688   "Start reading news in newsgroup GROUP.
3689 If SHOW-ALL is non-nil, already read articles are also listed.
3690 If NO-ARTICLE is non-nil, no article is selected initially.
3691 If NO-DISPLAY, don't generate a summary buffer."
3692   (let (result)
3693     (while (and group
3694                 (null (setq result
3695                             (let ((gnus-auto-select-next nil))
3696                               (or (gnus-summary-read-group-1
3697                                    group show-all no-article
3698                                    kill-buffer no-display
3699                                    select-articles)
3700                                   (setq show-all nil
3701                                         select-articles nil)))))
3702                 (eq gnus-auto-select-next 'quietly))
3703       (set-buffer gnus-group-buffer)
3704       ;; The entry function called above goes to the next
3705       ;; group automatically, so we go two groups back
3706       ;; if we are searching for the previous group.
3707       (when backward
3708         (gnus-group-prev-unread-group 2))
3709       (if (not (equal group (gnus-group-group-name)))
3710           (setq group (gnus-group-group-name))
3711         (setq group nil)))
3712     result))
3713
3714 (defun gnus-summary-read-group-1 (group show-all no-article
3715                                         kill-buffer no-display
3716                                         &optional select-articles)
3717   ;; Killed foreign groups can't be entered.
3718   ;;  (when (and (not (gnus-group-native-p group))
3719   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3720   ;;    (error "Dead non-native groups can't be entered"))
3721   (gnus-message 5 "Retrieving newsgroup: %s..."
3722                 (gnus-group-decoded-name group))
3723   (let* ((new-group (gnus-summary-setup-buffer group))
3724          (quit-config (gnus-group-quit-config group))
3725          (did-select (and new-group (gnus-select-newsgroup
3726                                      group show-all select-articles))))
3727     (cond
3728      ;; This summary buffer exists already, so we just select it.
3729      ((not new-group)
3730       (gnus-set-global-variables)
3731       (when kill-buffer
3732         (gnus-kill-or-deaden-summary kill-buffer))
3733       (gnus-configure-windows 'summary 'force)
3734       (gnus-set-mode-line 'summary)
3735       (gnus-summary-position-point)
3736       (message "")
3737       t)
3738      ;; We couldn't select this group.
3739      ((null did-select)
3740       (when (and (eq major-mode 'gnus-summary-mode)
3741                  (not (equal (current-buffer) kill-buffer)))
3742         (kill-buffer (current-buffer))
3743         (if (not quit-config)
3744             (progn
3745               ;; Update the info -- marks might need to be removed,
3746               ;; for instance.
3747               (gnus-summary-update-info)
3748               (set-buffer gnus-group-buffer)
3749               (gnus-group-jump-to-group group)
3750               (gnus-group-next-unread-group 1))
3751           (gnus-handle-ephemeral-exit quit-config)))
3752       (let ((grpinfo (gnus-get-info group)))
3753         (if (null (gnus-info-read grpinfo))
3754             (gnus-message 3 "Group %s contains no messages"
3755                           (gnus-group-decoded-name group))
3756           (gnus-message 3 "Can't select group")))
3757       nil)
3758      ;; The user did a `C-g' while prompting for number of articles,
3759      ;; so we exit this group.
3760      ((eq did-select 'quit)
3761       (and (eq major-mode 'gnus-summary-mode)
3762            (not (equal (current-buffer) kill-buffer))
3763            (kill-buffer (current-buffer)))
3764       (when kill-buffer
3765         (gnus-kill-or-deaden-summary kill-buffer))
3766       (if (not quit-config)
3767           (progn
3768             (set-buffer gnus-group-buffer)
3769             (gnus-group-jump-to-group group)
3770             (gnus-group-next-unread-group 1)
3771             (gnus-configure-windows 'group 'force))
3772         (gnus-handle-ephemeral-exit quit-config))
3773       ;; Finally signal the quit.
3774       (signal 'quit nil))
3775      ;; The group was successfully selected.
3776      (t
3777       (gnus-set-global-variables)
3778       ;; Save the active value in effect when the group was entered.
3779       (setq gnus-newsgroup-active
3780             (gnus-copy-sequence
3781              (gnus-active gnus-newsgroup-name)))
3782       ;; You can change the summary buffer in some way with this hook.
3783       (gnus-run-hooks 'gnus-select-group-hook)
3784       (when (memq 'summary (gnus-update-format-specifications
3785                             nil 'summary 'summary-mode 'summary-dummy))
3786         ;; The format specification for the summary line was updated,
3787         ;; so we need to update the mark positions as well.
3788         (gnus-update-summary-mark-positions))
3789       ;; Do score processing.
3790       (when gnus-use-scoring
3791         (gnus-possibly-score-headers))
3792       ;; Check whether to fill in the gaps in the threads.
3793       (when gnus-build-sparse-threads
3794         (gnus-build-sparse-threads))
3795       ;; Find the initial limit.
3796       (if show-all
3797           (let ((gnus-newsgroup-dormant nil))
3798             (gnus-summary-initial-limit show-all))
3799         (gnus-summary-initial-limit show-all))
3800       ;; Generate the summary buffer.
3801       (unless no-display
3802         (gnus-summary-prepare))
3803       (when gnus-use-trees
3804         (gnus-tree-open group)
3805         (setq gnus-summary-highlight-line-function
3806               'gnus-tree-highlight-article))
3807       ;; If the summary buffer is empty, but there are some low-scored
3808       ;; articles or some excluded dormants, we include these in the
3809       ;; buffer.
3810       (when (and (zerop (buffer-size))
3811                  (not no-display))
3812         (cond (gnus-newsgroup-dormant
3813                (gnus-summary-limit-include-dormant))
3814               ((and gnus-newsgroup-scored show-all)
3815                (gnus-summary-limit-include-expunged t))))
3816       ;; Function `gnus-apply-kill-file' must be called in this hook.
3817       (gnus-run-hooks 'gnus-apply-kill-hook)
3818       (if (and (zerop (buffer-size))
3819                (not no-display))
3820           (progn
3821             ;; This newsgroup is empty.
3822             (gnus-summary-catchup-and-exit nil t)
3823             (gnus-message 6 "No unread news")
3824             (when kill-buffer
3825               (gnus-kill-or-deaden-summary kill-buffer))
3826             ;; Return nil from this function.
3827             nil)
3828         ;; Hide conversation thread subtrees.  We cannot do this in
3829         ;; gnus-summary-prepare-hook since kill processing may not
3830         ;; work with hidden articles.
3831         (gnus-summary-maybe-hide-threads)
3832         (when kill-buffer
3833           (gnus-kill-or-deaden-summary kill-buffer))
3834         (gnus-summary-auto-select-subject)
3835         ;; Show first unread article if requested.
3836         (if (and (not no-article)
3837                  (not no-display)
3838                  gnus-newsgroup-unreads
3839                  gnus-auto-select-first)
3840             (progn
3841               (gnus-configure-windows 'summary)
3842               (let ((art (gnus-summary-article-number)))
3843                 (unless (and (not gnus-plugged)
3844                              (or (memq art gnus-newsgroup-undownloaded)
3845                                  (memq art gnus-newsgroup-downloadable)))
3846                   (gnus-summary-goto-article art))))
3847           ;; Don't select any articles.
3848           (gnus-summary-position-point)
3849           (gnus-configure-windows 'summary 'force)
3850           (gnus-set-mode-line 'summary))
3851         (when (and gnus-auto-center-group
3852                    (get-buffer-window gnus-group-buffer t))
3853           ;; Gotta use windows, because recenter does weird stuff if
3854           ;; the current buffer ain't the displayed window.
3855           (let ((owin (selected-window)))
3856             (select-window (get-buffer-window gnus-group-buffer t))
3857             (when (gnus-group-goto-group group)
3858               (recenter))
3859             (select-window owin)))
3860         ;; Mark this buffer as "prepared".
3861         (setq gnus-newsgroup-prepared t)
3862         (gnus-run-hooks 'gnus-summary-prepared-hook)
3863         (unless (gnus-ephemeral-group-p group)
3864           (gnus-group-update-group group))
3865         t)))))
3866
3867 (defun gnus-summary-auto-select-subject ()
3868   "Select the subject line on initial group entry."
3869   (goto-char (point-min))
3870   (cond
3871    ((eq gnus-auto-select-subject 'best)
3872     (gnus-summary-best-unread-subject))
3873    ((eq gnus-auto-select-subject 'unread)
3874     (gnus-summary-first-unread-subject))
3875    ((eq gnus-auto-select-subject 'unseen)
3876     (gnus-summary-first-unseen-subject))
3877    ((eq gnus-auto-select-subject 'unseen-or-unread)
3878     (gnus-summary-first-unseen-or-unread-subject))
3879    ((eq gnus-auto-select-subject 'first)
3880     ;; Do nothing.
3881     )
3882    ((functionp gnus-auto-select-subject)
3883     (funcall gnus-auto-select-subject))))
3884
3885 (defun gnus-summary-prepare ()
3886   "Generate the summary buffer."
3887   (interactive)
3888   (let ((buffer-read-only nil))
3889     (erase-buffer)
3890     (setq gnus-newsgroup-data nil
3891           gnus-newsgroup-data-reverse nil)
3892     (gnus-run-hooks 'gnus-summary-generate-hook)
3893     ;; Generate the buffer, either with threads or without.
3894     (when gnus-newsgroup-headers
3895       (gnus-summary-prepare-threads
3896        (if gnus-show-threads
3897            (gnus-sort-gathered-threads
3898             (funcall gnus-summary-thread-gathering-function
3899                      (gnus-sort-threads
3900                       (gnus-cut-threads (gnus-make-threads)))))
3901          ;; Unthreaded display.
3902          (gnus-sort-articles gnus-newsgroup-headers))))
3903     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3904     ;; Call hooks for modifying summary buffer.
3905     (goto-char (point-min))
3906     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3907
3908 (defsubst gnus-general-simplify-subject (subject)
3909   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3910   (setq subject
3911         (cond
3912          ;; Truncate the subject.
3913          (gnus-simplify-subject-functions
3914           (gnus-map-function gnus-simplify-subject-functions subject))
3915          ((numberp gnus-summary-gather-subject-limit)
3916           (setq subject (gnus-simplify-subject-re subject))
3917           (if (> (length subject) gnus-summary-gather-subject-limit)
3918               (substring subject 0 gnus-summary-gather-subject-limit)
3919             subject))
3920          ;; Fuzzily simplify it.
3921          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3922           (gnus-simplify-subject-fuzzy subject))
3923          ;; Just remove the leading "Re:".
3924          (t
3925           (gnus-simplify-subject-re subject))))
3926
3927   (if (and gnus-summary-gather-exclude-subject
3928            (string-match gnus-summary-gather-exclude-subject subject))
3929       nil                         ; This article shouldn't be gathered
3930     subject))
3931
3932 (defun gnus-summary-simplify-subject-query ()
3933   "Query where the respool algorithm would put this article."
3934   (interactive)
3935   (gnus-summary-select-article)
3936   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3937
3938 (defun gnus-gather-threads-by-subject (threads)
3939   "Gather threads by looking at Subject headers."
3940   (if (not gnus-summary-make-false-root)
3941       threads
3942     (let ((hashtb (gnus-make-hashtable 1024))
3943           (prev threads)
3944           (result threads)
3945           subject hthread whole-subject)
3946       (while threads
3947         (setq subject (gnus-general-simplify-subject
3948                        (setq whole-subject (mail-header-subject
3949                                             (caar threads)))))
3950         (when subject
3951           (if (setq hthread (gnus-gethash subject hashtb))
3952               (progn
3953                 ;; We enter a dummy root into the thread, if we
3954                 ;; haven't done that already.
3955                 (unless (stringp (caar hthread))
3956                   (setcar hthread (list whole-subject (car hthread))))
3957                 ;; We add this new gathered thread to this gathered
3958                 ;; thread.
3959                 (setcdr (car hthread)
3960                         (nconc (cdar hthread) (list (car threads))))
3961                 ;; Remove it from the list of threads.
3962                 (setcdr prev (cdr threads))
3963                 (setq threads prev))
3964             ;; Enter this thread into the hash table.
3965             (gnus-sethash subject
3966                           (if gnus-summary-make-false-root-always
3967                               (progn
3968                                 ;; If you want a dummy root above all
3969                                 ;; threads...
3970                                 (setcar threads (list whole-subject
3971                                                       (car threads)))
3972                                 threads)
3973                             threads)
3974                           hashtb)))
3975         (setq prev threads)
3976         (setq threads (cdr threads)))
3977       result)))
3978
3979 (defun gnus-gather-threads-by-references (threads)
3980   "Gather threads by looking at References headers."
3981   (let ((idhashtb (gnus-make-hashtable 1024))
3982         (thhashtb (gnus-make-hashtable 1024))
3983         (prev threads)
3984         (result threads)
3985         ids references id gthread gid entered ref)
3986     (while threads
3987       (when (setq references (mail-header-references (caar threads)))
3988         (setq id (mail-header-id (caar threads))
3989               ids (inline (gnus-split-references references))
3990               entered nil)
3991         (while (setq ref (pop ids))
3992           (setq ids (delete ref ids))
3993           (if (not (setq gid (gnus-gethash ref idhashtb)))
3994               (progn
3995                 (gnus-sethash ref id idhashtb)
3996                 (gnus-sethash id threads thhashtb))
3997             (setq gthread (gnus-gethash gid thhashtb))
3998             (unless entered
3999               ;; We enter a dummy root into the thread, if we
4000               ;; haven't done that already.
4001               (unless (stringp (caar gthread))
4002                 (setcar gthread (list (mail-header-subject (caar gthread))
4003                                       (car gthread))))
4004               ;; We add this new gathered thread to this gathered
4005               ;; thread.
4006               (setcdr (car gthread)
4007                       (nconc (cdar gthread) (list (car threads)))))
4008             ;; Add it into the thread hash table.
4009             (gnus-sethash id gthread thhashtb)
4010             (setq entered t)
4011             ;; Remove it from the list of threads.
4012             (setcdr prev (cdr threads))
4013             (setq threads prev))))
4014       (setq prev threads)
4015       (setq threads (cdr threads)))
4016     result))
4017
4018 (defun gnus-sort-gathered-threads (threads)
4019   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4020   (let ((result threads))
4021     (while threads
4022       (when (stringp (caar threads))
4023         (setcdr (car threads)
4024                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4025       (setq threads (cdr threads)))
4026     result))
4027
4028 (defun gnus-thread-loop-p (root thread)
4029   "Say whether ROOT is in THREAD."
4030   (let ((stack (list thread))
4031         (infloop 0)
4032         th)
4033     (while (setq thread (pop stack))
4034       (setq th (cdr thread))
4035       (while (and th
4036                   (not (eq (caar th) root)))
4037         (pop th))
4038       (if th
4039           ;; We have found a loop.
4040           (let (ref-dep)
4041             (setcdr thread (delq (car th) (cdr thread)))
4042             (if (boundp (setq ref-dep (intern "none"
4043                                               gnus-newsgroup-dependencies)))
4044                 (setcdr (symbol-value ref-dep)
4045                         (nconc (cdr (symbol-value ref-dep))
4046                                (list (car th))))
4047               (set ref-dep (list nil (car th))))
4048             (setq infloop 1
4049                   stack nil))
4050         ;; Push all the subthreads onto the stack.
4051         (push (cdr thread) stack)))
4052     infloop))
4053
4054 (defun gnus-make-threads ()
4055   "Go through the dependency hashtb and find the roots.  Return all threads."
4056   (let (threads)
4057     (while (catch 'infloop
4058              (mapatoms
4059               (lambda (refs)
4060                 ;; Deal with self-referencing References loops.
4061                 (when (and (car (symbol-value refs))
4062                            (not (zerop
4063                                  (apply
4064                                   '+
4065                                   (mapcar
4066                                    (lambda (thread)
4067                                      (gnus-thread-loop-p
4068                                       (car (symbol-value refs)) thread))
4069                                    (cdr (symbol-value refs)))))))
4070                   (setq threads nil)
4071                   (throw 'infloop t))
4072                 (unless (car (symbol-value refs))
4073                   ;; These threads do not refer back to any other
4074                   ;; articles, so they're roots.
4075                   (setq threads (append (cdr (symbol-value refs)) threads))))
4076               gnus-newsgroup-dependencies)))
4077     threads))
4078
4079 ;; Build the thread tree.
4080 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4081   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4082
4083 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4084 if it was already present.
4085
4086 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4087 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4088 Message-IDs will be renamed to a unique Message-ID before being
4089 entered.
4090
4091 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4092   (let* ((id (mail-header-id header))
4093          (id-dep (and id (intern id dependencies)))
4094          parent-id ref ref-dep ref-header replaced)
4095     ;; Enter this `header' in the `dependencies' table.
4096     (cond
4097      ((not id-dep)
4098       (setq header nil))
4099      ;; The first two cases do the normal part: enter a new `header'
4100      ;; in the `dependencies' table.
4101      ((not (boundp id-dep))
4102       (set id-dep (list header)))
4103      ((null (car (symbol-value id-dep)))
4104       (setcar (symbol-value id-dep) header))
4105
4106      ;; From here the `header' was already present in the
4107      ;; `dependencies' table.
4108      (force-new
4109       ;; Overrides an existing entry;
4110       ;; just set the header part of the entry.
4111       (setcar (symbol-value id-dep) header)
4112       (setq replaced t))
4113
4114      ;; Renames the existing `header' to a unique Message-ID.
4115      ((not gnus-summary-ignore-duplicates)
4116       ;; An article with this Message-ID has already been seen.
4117       ;; We rename the Message-ID.
4118       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4119            (list header))
4120       (mail-header-set-id header id))
4121
4122      ;; The last case ignores an existing entry, except it adds any
4123      ;; additional Xrefs (in case the two articles came from different
4124      ;; servers.
4125      ;; Also sets `header' to `nil' meaning that the `dependencies'
4126      ;; table was *not* modified.
4127      (t
4128       (mail-header-set-xref
4129        (car (symbol-value id-dep))
4130        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4131                    "")
4132                (or (mail-header-xref header) "")))
4133       (setq header nil)))
4134
4135     (when (and header (not replaced))
4136       ;; First check that we are not creating a References loop.
4137       (setq parent-id (gnus-parent-id (mail-header-references header)))
4138       (setq ref parent-id)
4139       (while (and ref
4140                   (setq ref-dep (intern-soft ref dependencies))
4141                   (boundp ref-dep)
4142                   (setq ref-header (car (symbol-value ref-dep))))
4143         (if (string= id ref)
4144             ;; Yuk!  This is a reference loop.  Make the article be a
4145             ;; root article.
4146             (progn
4147               (mail-header-set-references (car (symbol-value id-dep)) "none")
4148               (setq ref nil)
4149               (setq parent-id nil))
4150           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4151       (setq ref-dep (intern (or parent-id "none") dependencies))
4152       (if (boundp ref-dep)
4153           (setcdr (symbol-value ref-dep)
4154                   (nconc (cdr (symbol-value ref-dep))
4155                          (list (symbol-value id-dep))))
4156         (set ref-dep (list nil (symbol-value id-dep)))))
4157     header))
4158
4159 (defun gnus-extract-message-id-from-in-reply-to (string)
4160   (if (string-match "<[^>]+>" string)
4161       (substring string (match-beginning 0) (match-end 0))
4162     nil))
4163
4164 (defun gnus-build-sparse-threads ()
4165   (let ((headers gnus-newsgroup-headers)
4166         (mail-parse-charset gnus-newsgroup-charset)
4167         (gnus-summary-ignore-duplicates t)
4168         header references generation relations
4169         subject child end new-child date)
4170     ;; First we create an alist of generations/relations, where
4171     ;; generations is how much we trust the relation, and the relation
4172     ;; is parent/child.
4173     (gnus-message 7 "Making sparse threads...")
4174     (save-excursion
4175       (nnheader-set-temp-buffer " *gnus sparse threads*")
4176       (while (setq header (pop headers))
4177         (when (and (setq references (mail-header-references header))
4178                    (not (string= references "")))
4179           (insert references)
4180           (setq child (mail-header-id header)
4181                 subject (mail-header-subject header)
4182                 date (mail-header-date header)
4183                 generation 0)
4184           (while (search-backward ">" nil t)
4185             (setq end (1+ (point)))
4186             (when (search-backward "<" nil t)
4187               (setq new-child (buffer-substring (point) end))
4188               (push (list (incf generation)
4189                           child (setq child new-child)
4190                           subject date)
4191                     relations)))
4192           (when child
4193             (push (list (1+ generation) child nil subject) relations))
4194           (erase-buffer)))
4195       (kill-buffer (current-buffer)))
4196     ;; Sort over trustworthiness.
4197     (mapcar
4198      (lambda (relation)
4199        (when (gnus-dependencies-add-header
4200               (make-full-mail-header
4201                gnus-reffed-article-number
4202                (nth 3 relation) "" (or (nth 4 relation) "")
4203                (nth 1 relation)
4204                (or (nth 2 relation) "") 0 0 "")
4205               gnus-newsgroup-dependencies nil)
4206          (push gnus-reffed-article-number gnus-newsgroup-limit)
4207          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4208          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4209                gnus-newsgroup-reads)
4210          (decf gnus-reffed-article-number)))
4211      (sort relations 'car-less-than-car))
4212     (gnus-message 7 "Making sparse threads...done")))
4213
4214 (defun gnus-build-old-threads ()
4215   ;; Look at all the articles that refer back to old articles, and
4216   ;; fetch the headers for the articles that aren't there.  This will
4217   ;; build complete threads - if the roots haven't been expired by the
4218   ;; server, that is.
4219   (let ((mail-parse-charset gnus-newsgroup-charset)
4220         id heads)
4221     (mapatoms
4222      (lambda (refs)
4223        (when (not (car (symbol-value refs)))
4224          (setq heads (cdr (symbol-value refs)))
4225          (while heads
4226            (if (memq (mail-header-number (caar heads))
4227                      gnus-newsgroup-dormant)
4228                (setq heads (cdr heads))
4229              (setq id (symbol-name refs))
4230              (while (and (setq id (gnus-build-get-header id))
4231                          (not (car (gnus-id-to-thread id)))))
4232              (setq heads nil)))))
4233      gnus-newsgroup-dependencies)))
4234
4235 (defsubst gnus-remove-odd-characters (string)
4236   "Translate STRING into something that doesn't contain weird characters."
4237   (mm-subst-char-in-string
4238    ?\r ?\-
4239    (mm-subst-char-in-string ?\n ?\- string t) t))
4240
4241 ;; This function has to be called with point after the article number
4242 ;; on the beginning of the line.
4243 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4244   (let ((eol (point-at-eol))
4245         (buffer (current-buffer))
4246         header references in-reply-to)
4247
4248     ;; overview: [num subject from date id refs chars lines misc]
4249     (unwind-protect
4250         (let (x)
4251           (narrow-to-region (point) eol)
4252           (unless (eobp)
4253             (forward-char))
4254
4255           (setq header
4256                 (make-full-mail-header
4257                  number                 ; number
4258                  (condition-case ()     ; subject
4259                      (gnus-remove-odd-characters
4260                       (funcall gnus-decode-encoded-word-function
4261                                (setq x (nnheader-nov-field))))
4262                    (error x))
4263                  (condition-case ()     ; from
4264                      (gnus-remove-odd-characters
4265                       (funcall gnus-decode-encoded-word-function
4266                                (setq x (nnheader-nov-field))))
4267                    (error x))
4268                  (nnheader-nov-field)   ; date
4269                  (nnheader-nov-read-message-id number)  ; id
4270                  (setq references (nnheader-nov-field)) ; refs
4271                  (nnheader-nov-read-integer) ; chars
4272                  (nnheader-nov-read-integer) ; lines
4273                  (unless (eobp)
4274                    (if (looking-at "Xref: ")
4275                        (goto-char (match-end 0)))
4276                    (nnheader-nov-field)) ; Xref
4277                  (nnheader-nov-parse-extra)))) ; extra
4278
4279       (widen))
4280
4281     (when (and (string= references "")
4282                (setq in-reply-to (mail-header-extra header))
4283                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4284       (mail-header-set-references
4285        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4286
4287     (when gnus-alter-header-function
4288       (funcall gnus-alter-header-function header))
4289     (gnus-dependencies-add-header header dependencies force-new)))
4290
4291 (defun gnus-build-get-header (id)
4292   "Look through the buffer of NOV lines and find the header to ID.
4293 Enter this line into the dependencies hash table, and return
4294 the id of the parent article (if any)."
4295   (let ((deps gnus-newsgroup-dependencies)
4296         found header)
4297     (prog1
4298         (save-excursion
4299           (set-buffer nntp-server-buffer)
4300           (let ((case-fold-search nil))
4301             (goto-char (point-min))
4302             (while (and (not found)
4303                         (search-forward id nil t))
4304               (beginning-of-line)
4305               (setq found (looking-at
4306                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4307                                    (regexp-quote id))))
4308               (or found (beginning-of-line 2)))
4309             (when found
4310               (beginning-of-line)
4311               (and
4312                (setq header (gnus-nov-parse-line
4313                              (read (current-buffer)) deps))
4314                (gnus-parent-id (mail-header-references header))))))
4315       (when header
4316         (let ((number (mail-header-number header)))
4317           (push number gnus-newsgroup-limit)
4318           (push header gnus-newsgroup-headers)
4319           (if (memq number gnus-newsgroup-unselected)
4320               (progn
4321                 (setq gnus-newsgroup-unreads
4322                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4323                                                number))
4324                 (setq gnus-newsgroup-unselected
4325                       (delq number gnus-newsgroup-unselected)))
4326             (push number gnus-newsgroup-ancient)))))))
4327
4328 (defun gnus-build-all-threads ()
4329   "Read all the headers."
4330   (let ((gnus-summary-ignore-duplicates t)
4331         (mail-parse-charset gnus-newsgroup-charset)
4332         (dependencies gnus-newsgroup-dependencies)
4333         header article)
4334     (save-excursion
4335       (set-buffer nntp-server-buffer)
4336       (let ((case-fold-search nil))
4337         (goto-char (point-min))
4338         (while (not (eobp))
4339           (ignore-errors
4340             (setq article (read (current-buffer))
4341                   header (gnus-nov-parse-line article dependencies)))
4342           (when header
4343             (with-current-buffer gnus-summary-buffer
4344               (push header gnus-newsgroup-headers)
4345               (if (memq (setq article (mail-header-number header))
4346                         gnus-newsgroup-unselected)
4347                   (progn
4348                     (setq gnus-newsgroup-unreads
4349                           (gnus-add-to-sorted-list
4350                            gnus-newsgroup-unreads article))
4351                     (setq gnus-newsgroup-unselected
4352                           (delq article gnus-newsgroup-unselected)))
4353                 (push article gnus-newsgroup-ancient)))
4354             (forward-line 1)))))))
4355
4356 (defun gnus-summary-update-article-line (article header)
4357   "Update the line for ARTICLE using HEADER."
4358   (let* ((id (mail-header-id header))
4359          (thread (gnus-id-to-thread id)))
4360     (unless thread
4361       (error "Article in no thread"))
4362     ;; Update the thread.
4363     (setcar thread header)
4364     (gnus-summary-goto-subject article)
4365     (let* ((datal (gnus-data-find-list article))
4366            (data (car datal))
4367            (buffer-read-only nil)
4368            (level (gnus-summary-thread-level)))
4369       (gnus-delete-line)
4370       (let ((inserted (- (point)
4371                          (progn
4372                            (gnus-summary-insert-line
4373                             header level nil
4374                             (memq article gnus-newsgroup-undownloaded)
4375                             (gnus-article-mark article)
4376                             (memq article gnus-newsgroup-replied)
4377                             (memq article gnus-newsgroup-expirable)
4378                             ;; Only insert the Subject string when it's different
4379                             ;; from the previous Subject string.
4380                             (if (and
4381                                  gnus-show-threads
4382                                  (gnus-subject-equal
4383                                   (condition-case ()
4384                                       (mail-header-subject
4385                                        (gnus-data-header
4386                                         (cadr
4387                                          (gnus-data-find-list
4388                                           article
4389                                           (gnus-data-list t)))))
4390                                     ;; Error on the side of excessive subjects.
4391                                     (error ""))
4392                                   (mail-header-subject header)))
4393                                 ""
4394                               (mail-header-subject header))
4395                             nil (cdr (assq article gnus-newsgroup-scored))
4396                             (memq article gnus-newsgroup-processable))
4397                            (point)))))
4398         (when (cdr datal)
4399           (gnus-data-update-list
4400            (cdr datal)
4401            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4402
4403 (defun gnus-summary-update-article (article &optional iheader)
4404   "Update ARTICLE in the summary buffer."
4405   (set-buffer gnus-summary-buffer)
4406   (let* ((header (gnus-summary-article-header article))
4407          (id (mail-header-id header))
4408          (data (gnus-data-find article))
4409          (thread (gnus-id-to-thread id))
4410          (references (mail-header-references header))
4411          (parent
4412           (gnus-id-to-thread
4413            (or (gnus-parent-id
4414                 (when (and references
4415                            (not (equal "" references)))
4416                   references))
4417                "none")))
4418          (buffer-read-only nil)
4419          (old (car thread)))
4420     (when thread
4421       (unless iheader
4422         (setcar thread nil)
4423         (when parent
4424           (delq thread parent)))
4425       (if (gnus-summary-insert-subject id header)
4426           ;; Set the (possibly) new article number in the data structure.
4427           (gnus-data-set-number data (gnus-id-to-article id))
4428         (setcar thread old)
4429         nil))))
4430
4431 (defun gnus-rebuild-thread (id &optional line)
4432   "Rebuild the thread containing ID.
4433 If LINE, insert the rebuilt thread starting on line LINE."
4434   (let ((buffer-read-only nil)
4435         old-pos current thread data)
4436     (if (not gnus-show-threads)
4437         (setq thread (list (car (gnus-id-to-thread id))))
4438       ;; Get the thread this article is part of.
4439       (setq thread (gnus-remove-thread id)))
4440     (setq old-pos (point-at-bol))
4441     (setq current (save-excursion
4442                     (and (re-search-backward "[\r\n]" nil t)
4443                          (gnus-summary-article-number))))
4444     ;; If this is a gathered thread, we have to go some re-gathering.
4445     (when (stringp (car thread))
4446       (let ((subject (car thread))
4447             roots thr)
4448         (setq thread (cdr thread))
4449         (while thread
4450           (unless (memq (setq thr (gnus-id-to-thread
4451                                    (gnus-root-id
4452                                     (mail-header-id (caar thread)))))
4453                         roots)
4454             (push thr roots))
4455           (setq thread (cdr thread)))
4456         ;; We now have all (unique) roots.
4457         (if (= (length roots) 1)
4458             ;; All the loose roots are now one solid root.
4459             (setq thread (car roots))
4460           (setq thread (cons subject (gnus-sort-threads roots))))))
4461     (let (threads)
4462       ;; We then insert this thread into the summary buffer.
4463       (when line
4464         (goto-char (point-min))
4465         (forward-line (1- line)))
4466       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4467         (if gnus-show-threads
4468             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4469           (gnus-summary-prepare-unthreaded thread))
4470         (setq data (nreverse gnus-newsgroup-data))
4471         (setq threads gnus-newsgroup-threads))
4472       ;; We splice the new data into the data structure.
4473       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4474       ;;!!! then we want to insert at the beginning of the buffer.
4475       ;;!!! That happens to be true with Gnus now, but that may
4476       ;;!!! change in the future.  Perhaps.
4477       (gnus-data-enter-list
4478        (if line nil current) data (- (point) old-pos))
4479       (setq gnus-newsgroup-threads
4480             (nconc threads gnus-newsgroup-threads))
4481       (gnus-data-compute-positions))))
4482
4483 (defun gnus-number-to-header (number)
4484   "Return the header for article NUMBER."
4485   (let ((headers gnus-newsgroup-headers))
4486     (while (and headers
4487                 (not (= number (mail-header-number (car headers)))))
4488       (pop headers))
4489     (when headers
4490       (car headers))))
4491
4492 (defun gnus-parent-headers (in-headers &optional generation)
4493   "Return the headers of the GENERATIONeth parent of HEADERS."
4494   (unless generation
4495     (setq generation 1))
4496   (let ((parent t)
4497         (headers in-headers)
4498         references)
4499     (while (and parent
4500                 (not (zerop generation))
4501                 (setq references (mail-header-references headers)))
4502       (setq headers (if (and references
4503                              (setq parent (gnus-parent-id references)))
4504                         (car (gnus-id-to-thread parent))
4505                       nil))
4506       (decf generation))
4507     (and (not (eq headers in-headers))
4508          headers)))
4509
4510 (defun gnus-id-to-thread (id)
4511   "Return the (sub-)thread where ID appears."
4512   (gnus-gethash id gnus-newsgroup-dependencies))
4513
4514 (defun gnus-id-to-article (id)
4515   "Return the article number of ID."
4516   (let ((thread (gnus-id-to-thread id)))
4517     (when (and thread
4518                (car thread))
4519       (mail-header-number (car thread)))))
4520
4521 (defun gnus-id-to-header (id)
4522   "Return the article headers of ID."
4523   (car (gnus-id-to-thread id)))
4524
4525 (defun gnus-article-displayed-root-p (article)
4526   "Say whether ARTICLE is a root(ish) article."
4527   (let ((level (gnus-summary-thread-level article))
4528         (refs (mail-header-references  (gnus-summary-article-header article)))
4529         particle)
4530     (cond
4531      ((null level) nil)
4532      ((zerop level) t)
4533      ((null refs) t)
4534      ((null (gnus-parent-id refs)) t)
4535      ((and (= 1 level)
4536            (null (setq particle (gnus-id-to-article
4537                                  (gnus-parent-id refs))))
4538            (null (gnus-summary-thread-level particle)))))))
4539
4540 (defun gnus-root-id (id)
4541   "Return the id of the root of the thread where ID appears."
4542   (let (last-id prev)
4543     (while (and id (setq prev (car (gnus-id-to-thread id))))
4544       (setq last-id id
4545             id (gnus-parent-id (mail-header-references prev))))
4546     last-id))
4547
4548 (defun gnus-articles-in-thread (thread)
4549   "Return the list of articles in THREAD."
4550   (cons (mail-header-number (car thread))
4551         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4552
4553 (defun gnus-remove-thread (id &optional dont-remove)
4554   "Remove the thread that has ID in it."
4555   (let (headers thread last-id)
4556     ;; First go up in this thread until we find the root.
4557     (setq last-id (gnus-root-id id)
4558           headers (message-flatten-list (gnus-id-to-thread last-id)))
4559     ;; We have now found the real root of this thread.  It might have
4560     ;; been gathered into some loose thread, so we have to search
4561     ;; through the threads to find the thread we wanted.
4562     (let ((threads gnus-newsgroup-threads)
4563           sub)
4564       (while threads
4565         (setq sub (car threads))
4566         (if (stringp (car sub))
4567             ;; This is a gathered thread, so we look at the roots
4568             ;; below it to find whether this article is in this
4569             ;; gathered root.
4570             (progn
4571               (setq sub (cdr sub))
4572               (while sub
4573                 (when (member (caar sub) headers)
4574                   (setq thread (car threads)
4575                         threads nil
4576                         sub nil))
4577                 (setq sub (cdr sub))))
4578           ;; It's an ordinary thread, so we check it.
4579           (when (eq (car sub) (car headers))
4580             (setq thread sub
4581                   threads nil)))
4582         (setq threads (cdr threads)))
4583       ;; If this article is in no thread, then it's a root.
4584       (if thread
4585           (unless dont-remove
4586             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4587         (setq thread (gnus-id-to-thread last-id)))
4588       (when thread
4589         (prog1
4590             thread                      ; We return this thread.
4591           (unless dont-remove
4592             (if (stringp (car thread))
4593                 (progn
4594                   ;; If we use dummy roots, then we have to remove the
4595                   ;; dummy root as well.
4596                   (when (eq gnus-summary-make-false-root 'dummy)
4597                     ;; We go to the dummy root by going to
4598                     ;; the first sub-"thread", and then one line up.
4599                     (gnus-summary-goto-article
4600                      (mail-header-number (caadr thread)))
4601                     (forward-line -1)
4602                     (gnus-delete-line)
4603                     (gnus-data-compute-positions))
4604                   (setq thread (cdr thread))
4605                   (while thread
4606                     (gnus-remove-thread-1 (car thread))
4607                     (setq thread (cdr thread))))
4608               (gnus-remove-thread-1 thread))))))))
4609
4610 (defun gnus-remove-thread-1 (thread)
4611   "Remove the thread THREAD recursively."
4612   (let ((number (mail-header-number (pop thread)))
4613         d)
4614     (setq thread (reverse thread))
4615     (while thread
4616       (gnus-remove-thread-1 (pop thread)))
4617     (when (setq d (gnus-data-find number))
4618       (goto-char (gnus-data-pos d))
4619       (gnus-summary-show-thread)
4620       (gnus-data-remove
4621        number
4622        (- (point-at-bol)
4623           (prog1
4624               (1+ (point-at-eol))
4625             (gnus-delete-line)))))))
4626
4627 (defun gnus-sort-threads-1 (threads func)
4628   (sort (mapcar (lambda (thread)
4629                   (cons (car thread)
4630                         (and (cdr thread)
4631                              (gnus-sort-threads-1 (cdr thread) func))))
4632                 threads) func))
4633
4634 (defun gnus-sort-threads (threads)
4635   "Sort THREADS."
4636   (if (not gnus-thread-sort-functions)
4637       threads
4638     (gnus-message 8 "Sorting threads...")
4639     (let ((max-lisp-eval-depth 5000))
4640       (prog1 (gnus-sort-threads-1
4641          threads
4642          (gnus-make-sort-function gnus-thread-sort-functions))
4643         (gnus-message 8 "Sorting threads...done")))))
4644
4645 (defun gnus-sort-articles (articles)
4646   "Sort ARTICLES."
4647   (when gnus-article-sort-functions
4648     (gnus-message 7 "Sorting articles...")
4649     (prog1
4650         (setq gnus-newsgroup-headers
4651               (sort articles (gnus-make-sort-function
4652                               gnus-article-sort-functions)))
4653       (gnus-message 7 "Sorting articles...done"))))
4654
4655 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4656 (defmacro gnus-thread-header (thread)
4657   "Return header of first article in THREAD.
4658 Note that THREAD must never, ever be anything else than a variable -
4659 using some other form will lead to serious barfage."
4660   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4661   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4662   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4663         (vector thread) 2))
4664
4665 (defsubst gnus-article-sort-by-number (h1 h2)
4666   "Sort articles by article number."
4667   (< (mail-header-number h1)
4668      (mail-header-number h2)))
4669
4670 (defun gnus-thread-sort-by-number (h1 h2)
4671   "Sort threads by root article number."
4672   (gnus-article-sort-by-number
4673    (gnus-thread-header h1) (gnus-thread-header h2)))
4674
4675 (defsubst gnus-article-sort-by-random (h1 h2)
4676   "Sort articles by article number."
4677   (zerop (random 2)))
4678
4679 (defun gnus-thread-sort-by-random (h1 h2)
4680   "Sort threads by root article number."
4681   (gnus-article-sort-by-random
4682    (gnus-thread-header h1) (gnus-thread-header h2)))
4683
4684 (defsubst gnus-article-sort-by-lines (h1 h2)
4685   "Sort articles by article Lines header."
4686   (< (mail-header-lines h1)
4687      (mail-header-lines h2)))
4688
4689 (defun gnus-thread-sort-by-lines (h1 h2)
4690   "Sort threads by root article Lines header."
4691   (gnus-article-sort-by-lines
4692    (gnus-thread-header h1) (gnus-thread-header h2)))
4693
4694 (defsubst gnus-article-sort-by-chars (h1 h2)
4695   "Sort articles by octet length."
4696   (< (mail-header-chars h1)
4697      (mail-header-chars h2)))
4698
4699 (defun gnus-thread-sort-by-chars (h1 h2)
4700   "Sort threads by root article octet length."
4701   (gnus-article-sort-by-chars
4702    (gnus-thread-header h1) (gnus-thread-header h2)))
4703
4704 (defsubst gnus-article-sort-by-author (h1 h2)
4705   "Sort articles by root author."
4706   (string-lessp
4707    (let ((extract (funcall
4708                    gnus-extract-address-components
4709                    (mail-header-from h1))))
4710      (or (car extract) (cadr extract) ""))
4711    (let ((extract (funcall
4712                    gnus-extract-address-components
4713                    (mail-header-from h2))))
4714      (or (car extract) (cadr extract) ""))))
4715
4716 (defun gnus-thread-sort-by-author (h1 h2)
4717   "Sort threads by root author."
4718   (gnus-article-sort-by-author
4719    (gnus-thread-header h1)  (gnus-thread-header h2)))
4720
4721 (defsubst gnus-article-sort-by-recipient (h1 h2)
4722   "Sort articles by recipient."
4723   (string-lessp
4724    (let ((extract (funcall
4725                    gnus-extract-address-components
4726                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4727      (or (car extract) (cadr extract)))
4728    (let ((extract (funcall
4729                    gnus-extract-address-components
4730                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4731      (or (car extract) (cadr extract)))))
4732
4733 (defun gnus-thread-sort-by-recipient (h1 h2)
4734   "Sort threads by root recipient."
4735   (gnus-article-sort-by-recipient
4736    (gnus-thread-header h1) (gnus-thread-header h2)))
4737
4738 (defsubst gnus-article-sort-by-subject (h1 h2)
4739   "Sort articles by root subject."
4740   (string-lessp
4741    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4742    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4743
4744 (defun gnus-thread-sort-by-subject (h1 h2)
4745   "Sort threads by root subject."
4746   (gnus-article-sort-by-subject
4747    (gnus-thread-header h1) (gnus-thread-header h2)))
4748
4749 (defsubst gnus-article-sort-by-date (h1 h2)
4750   "Sort articles by root article date."
4751   (time-less-p
4752    (gnus-date-get-time (mail-header-date h1))
4753    (gnus-date-get-time (mail-header-date h2))))
4754
4755 (defun gnus-thread-sort-by-date (h1 h2)
4756   "Sort threads by root article date."
4757   (gnus-article-sort-by-date
4758    (gnus-thread-header h1) (gnus-thread-header h2)))
4759
4760 (defsubst gnus-article-sort-by-score (h1 h2)
4761   "Sort articles by root article score.
4762 Unscored articles will be counted as having a score of zero."
4763   (> (or (cdr (assq (mail-header-number h1)
4764                     gnus-newsgroup-scored))
4765          gnus-summary-default-score 0)
4766      (or (cdr (assq (mail-header-number h2)
4767                     gnus-newsgroup-scored))
4768          gnus-summary-default-score 0)))
4769
4770 (defun gnus-thread-sort-by-score (h1 h2)
4771   "Sort threads by root article score."
4772   (gnus-article-sort-by-score
4773    (gnus-thread-header h1) (gnus-thread-header h2)))
4774
4775 (defun gnus-thread-sort-by-total-score (h1 h2)
4776   "Sort threads by the sum of all scores in the thread.
4777 Unscored articles will be counted as having a score of zero."
4778   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4779
4780 (defun gnus-thread-total-score (thread)
4781   ;; This function find the total score of THREAD.
4782   (cond
4783    ((null thread)
4784     0)
4785    ((consp thread)
4786     (if (stringp (car thread))
4787         (apply gnus-thread-score-function 0
4788                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4789       (gnus-thread-total-score-1 thread)))
4790    (t
4791     (gnus-thread-total-score-1 (list thread)))))
4792
4793 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4794   "Sort threads such that the thread with the most recently arrived article comes first."
4795   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4796
4797 (defun gnus-thread-highest-number (thread)
4798   "Return the highest article number in THREAD."
4799   (apply 'max (mapcar (lambda (header)
4800                         (mail-header-number header))
4801                       (message-flatten-list thread))))
4802
4803 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4804   "Sort threads such that the thread with the most recently dated article comes first."
4805   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4806
4807 (defun gnus-thread-latest-date (thread)
4808   "Return the highest article date in THREAD."
4809   (let ((previous-time 0))
4810     (apply 'max
4811            (mapcar
4812             (lambda (header)
4813               (setq previous-time
4814                     (condition-case ()
4815                         (time-to-seconds (mail-header-parse-date
4816                                           (mail-header-date header)))
4817                       (error previous-time))))
4818             (sort
4819              (message-flatten-list thread)
4820              (lambda (h1 h2)
4821                (< (mail-header-number h1)
4822                   (mail-header-number h2))))))))
4823
4824 (defun gnus-thread-total-score-1 (root)
4825   ;; This function find the total score of the thread below ROOT.
4826   (setq root (car root))
4827   (apply gnus-thread-score-function
4828          (or (append
4829               (mapcar 'gnus-thread-total-score
4830                       (cdr (gnus-id-to-thread (mail-header-id root))))
4831               (when (> (mail-header-number root) 0)
4832                 (list (or (cdr (assq (mail-header-number root)
4833                                      gnus-newsgroup-scored))
4834                           gnus-summary-default-score 0))))
4835              (list gnus-summary-default-score)
4836              '(0))))
4837
4838 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4839 (defvar gnus-tmp-prev-subject nil)
4840 (defvar gnus-tmp-false-parent nil)
4841 (defvar gnus-tmp-root-expunged nil)
4842 (defvar gnus-tmp-dummy-line nil)
4843
4844 (eval-when-compile (defvar gnus-tmp-header))
4845 (defun gnus-extra-header (type &optional header)
4846   "Return the extra header of TYPE."
4847   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4848       ""))
4849
4850 (defvar gnus-tmp-thread-tree-header-string "")
4851
4852 (defcustom gnus-sum-thread-tree-root "> "
4853   "With %B spec, used for the root of a thread.
4854 If nil, use subject instead."
4855   :version "22.1"
4856   :type '(radio (const :format "%v  " nil) string)
4857   :group 'gnus-thread)
4858
4859 (defcustom gnus-sum-thread-tree-false-root "> "
4860   "With %B spec, used for a false root of a thread.
4861 If nil, use subject instead."
4862   :version "22.1"
4863   :type '(radio (const :format "%v  " nil) string)
4864   :group 'gnus-thread)
4865
4866 (defcustom gnus-sum-thread-tree-single-indent ""
4867   "With %B spec, used for a thread with just one message.
4868 If nil, use subject instead."
4869   :version "22.1"
4870   :type '(radio (const :format "%v  " nil) string)
4871   :group 'gnus-thread)
4872
4873 (defcustom gnus-sum-thread-tree-vertical "| "
4874   "With %B spec, used for drawing a vertical line."
4875   :version "22.1"
4876   :type 'string
4877   :group 'gnus-thread)
4878
4879 (defcustom gnus-sum-thread-tree-indent "  "
4880   "With %B spec, used for indenting."
4881   :version "22.1"
4882   :type 'string
4883   :group 'gnus-thread)
4884
4885 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4886   "With %B spec, used for a leaf with brothers."
4887   :version "22.1"
4888   :type 'string
4889   :group 'gnus-thread)
4890
4891 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4892   "With %B spec, used for a leaf without brothers."
4893   :version "22.1"
4894   :type 'string
4895   :group 'gnus-thread)
4896
4897 (defun gnus-summary-prepare-threads (threads)
4898   "Prepare summary buffer from THREADS and indentation LEVEL.
4899 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4900 or a straight list of headers."
4901   (gnus-message 7 "Generating summary...")
4902
4903   (setq gnus-newsgroup-threads threads)
4904   (beginning-of-line)
4905
4906   (let ((gnus-tmp-level 0)
4907         (default-score (or gnus-summary-default-score 0))
4908         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4909         (building-line-count gnus-summary-display-while-building)
4910         (building-count (integerp gnus-summary-display-while-building))
4911         thread number subject stack state gnus-tmp-gathered beg-match
4912         new-roots gnus-tmp-new-adopts thread-end simp-subject
4913         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4914         gnus-tmp-replied gnus-tmp-subject-or-nil
4915         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4916         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4917         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4918         tree-stack)
4919
4920     (setq gnus-tmp-prev-subject nil
4921           gnus-tmp-thread-tree-header-string "")
4922
4923     (if (vectorp (car threads))
4924         ;; If this is a straight (sic) list of headers, then a
4925         ;; threaded summary display isn't required, so we just create
4926         ;; an unthreaded one.
4927         (gnus-summary-prepare-unthreaded threads)
4928
4929       ;; Do the threaded display.
4930
4931       (if gnus-summary-display-while-building
4932           (switch-to-buffer (buffer-name)))
4933       (while (or threads stack gnus-tmp-new-adopts new-roots)
4934
4935         (if (and (= gnus-tmp-level 0)
4936                  (or (not stack)
4937                      (= (caar stack) 0))
4938                  (not gnus-tmp-false-parent)
4939                  (or gnus-tmp-new-adopts new-roots))
4940             (if gnus-tmp-new-adopts
4941                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4942                       thread (list (car gnus-tmp-new-adopts))
4943                       gnus-tmp-header (caar thread)
4944                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4945               (when new-roots
4946                 (setq thread (list (car new-roots))
4947                       gnus-tmp-header (caar thread)
4948                       new-roots (cdr new-roots))))
4949
4950           (if threads
4951               ;; If there are some threads, we do them before the
4952               ;; threads on the stack.
4953               (setq thread threads
4954                     gnus-tmp-header (caar thread))
4955             ;; There were no current threads, so we pop something off
4956             ;; the stack.
4957             (setq state (car stack)
4958                   gnus-tmp-level (car state)
4959                   tree-stack (cadr state)
4960                   thread (caddr state)
4961                   stack (cdr stack)
4962                   gnus-tmp-header (caar thread))))
4963
4964         (setq gnus-tmp-false-parent nil)
4965         (setq gnus-tmp-root-expunged nil)
4966         (setq thread-end nil)
4967
4968         (if (stringp gnus-tmp-header)
4969             ;; The header is a dummy root.
4970             (cond
4971              ((eq gnus-summary-make-false-root 'adopt)
4972               ;; We let the first article adopt the rest.
4973               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4974                                                (cddar thread)))
4975               (setq gnus-tmp-gathered
4976                     (nconc (mapcar
4977                             (lambda (h) (mail-header-number (car h)))
4978                             (cddar thread))
4979                            gnus-tmp-gathered))
4980               (setq thread (cons (list (caar thread)
4981                                        (cadar thread))
4982                                  (cdr thread)))
4983               (setq gnus-tmp-level -1
4984                     gnus-tmp-false-parent t))
4985              ((eq gnus-summary-make-false-root 'empty)
4986               ;; We print adopted articles with empty subject fields.
4987               (setq gnus-tmp-gathered
4988                     (nconc (mapcar
4989                             (lambda (h) (mail-header-number (car h)))
4990                             (cddar thread))
4991                            gnus-tmp-gathered))
4992               (setq gnus-tmp-level -1))
4993              ((eq gnus-summary-make-false-root 'dummy)
4994               ;; We remember that we probably want to output a dummy
4995               ;; root.
4996               (setq gnus-tmp-dummy-line gnus-tmp-header)
4997               (setq gnus-tmp-prev-subject gnus-tmp-header))
4998              (t
4999               ;; We do not make a root for the gathered
5000               ;; sub-threads at all.
5001               (setq gnus-tmp-level -1)))
5002
5003           (setq number (mail-header-number gnus-tmp-header)
5004                 subject (mail-header-subject gnus-tmp-header)
5005                 simp-subject (gnus-simplify-subject-fully subject))
5006
5007           (cond
5008            ;; If the thread has changed subject, we might want to make
5009            ;; this subthread into a root.
5010            ((and (null gnus-thread-ignore-subject)
5011                  (not (zerop gnus-tmp-level))
5012                  gnus-tmp-prev-subject
5013                  (not (string= gnus-tmp-prev-subject simp-subject)))
5014             (setq new-roots (nconc new-roots (list (car thread)))
5015                   thread-end t
5016                   gnus-tmp-header nil))
5017            ;; If the article lies outside the current limit,
5018            ;; then we do not display it.
5019            ((not (memq number gnus-newsgroup-limit))
5020             (setq gnus-tmp-gathered
5021                   (nconc (mapcar
5022                           (lambda (h) (mail-header-number (car h)))
5023                           (cdar thread))
5024                          gnus-tmp-gathered))
5025             (setq gnus-tmp-new-adopts (if (cdar thread)
5026                                           (append gnus-tmp-new-adopts
5027                                                   (cdar thread))
5028                                         gnus-tmp-new-adopts)
5029                   thread-end t
5030                   gnus-tmp-header nil)
5031             (when (zerop gnus-tmp-level)
5032               (setq gnus-tmp-root-expunged t)))
5033            ;; Perhaps this article is to be marked as read?
5034            ((and gnus-summary-mark-below
5035                  (< (or (cdr (assq number gnus-newsgroup-scored))
5036                         default-score)
5037                     gnus-summary-mark-below)
5038                  ;; Don't touch sparse articles.
5039                  (not (gnus-summary-article-sparse-p number))
5040                  (not (gnus-summary-article-ancient-p number)))
5041             (setq gnus-newsgroup-unreads
5042                   (delq number gnus-newsgroup-unreads))
5043             (if gnus-newsgroup-auto-expire
5044                 (setq gnus-newsgroup-expirable
5045                       (gnus-add-to-sorted-list
5046                        gnus-newsgroup-expirable number))
5047               (push (cons number gnus-low-score-mark)
5048                     gnus-newsgroup-reads))))
5049
5050           (when gnus-tmp-header
5051             ;; We may have an old dummy line to output before this
5052             ;; article.
5053             (when (and gnus-tmp-dummy-line
5054                        (gnus-subject-equal
5055                         gnus-tmp-dummy-line
5056                         (mail-header-subject gnus-tmp-header)))
5057               (gnus-summary-insert-dummy-line
5058                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5059               (setq gnus-tmp-dummy-line nil))
5060
5061             ;; Compute the mark.
5062             (setq gnus-tmp-unread (gnus-article-mark number))
5063
5064             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5065                                   gnus-tmp-header gnus-tmp-level)
5066                   gnus-newsgroup-data)
5067
5068             ;; Actually insert the line.
5069             (setq
5070              gnus-tmp-subject-or-nil
5071              (cond
5072               ((and gnus-thread-ignore-subject
5073                     gnus-tmp-prev-subject
5074                     (not (string= gnus-tmp-prev-subject simp-subject)))
5075                subject)
5076               ((zerop gnus-tmp-level)
5077                (if (and (eq gnus-summary-make-false-root 'empty)
5078                         (memq number gnus-tmp-gathered)
5079                         gnus-tmp-prev-subject
5080                         (string= gnus-tmp-prev-subject simp-subject))
5081                    gnus-summary-same-subject
5082                  subject))
5083               (t gnus-summary-same-subject)))
5084             (if (and (eq gnus-summary-make-false-root 'adopt)
5085                      (= gnus-tmp-level 1)
5086                      (memq number gnus-tmp-gathered))
5087                 (setq gnus-tmp-opening-bracket ?\<
5088                       gnus-tmp-closing-bracket ?\>)
5089               (setq gnus-tmp-opening-bracket ?\[
5090                     gnus-tmp-closing-bracket ?\]))
5091             (setq
5092              gnus-tmp-indentation
5093              (aref gnus-thread-indent-array gnus-tmp-level)
5094              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5095              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5096                                 gnus-summary-default-score 0)
5097              gnus-tmp-score-char
5098              (if (or (null gnus-summary-default-score)
5099                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5100                          gnus-summary-zcore-fuzz))
5101                  ?                      ;Whitespace
5102                (if (< gnus-tmp-score gnus-summary-default-score)
5103                    gnus-score-below-mark gnus-score-over-mark))
5104              gnus-tmp-replied
5105              (cond ((memq number gnus-newsgroup-processable)
5106                     gnus-process-mark)
5107                    ((memq number gnus-newsgroup-cached)
5108                     gnus-cached-mark)
5109                    ((memq number gnus-newsgroup-replied)
5110                     gnus-replied-mark)
5111                    ((memq number gnus-newsgroup-forwarded)
5112                     gnus-forwarded-mark)
5113                    ((memq number gnus-newsgroup-saved)
5114                     gnus-saved-mark)
5115                    ((memq number gnus-newsgroup-recent)
5116                     gnus-recent-mark)
5117                    ((memq number gnus-newsgroup-unseen)
5118                     gnus-unseen-mark)
5119                    (t gnus-no-mark))
5120              gnus-tmp-downloaded
5121              (cond ((memq number gnus-newsgroup-undownloaded)
5122                     gnus-undownloaded-mark)
5123                    (gnus-newsgroup-agentized
5124                     gnus-downloaded-mark)
5125                    (t
5126                     gnus-no-mark))
5127              gnus-tmp-from (mail-header-from gnus-tmp-header)
5128              gnus-tmp-name
5129              (cond
5130               ((string-match "<[^>]+> *$" gnus-tmp-from)
5131                (setq beg-match (match-beginning 0))
5132                (or (and (string-match "^\".+\"" gnus-tmp-from)
5133                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5134                    (substring gnus-tmp-from 0 beg-match)))
5135               ((string-match "(.+)" gnus-tmp-from)
5136                (substring gnus-tmp-from
5137                           (1+ (match-beginning 0)) (1- (match-end 0))))
5138               (t gnus-tmp-from))
5139
5140              ;; Do the %B string
5141              gnus-tmp-thread-tree-header-string
5142              (cond
5143               ((not gnus-show-threads) "")
5144               ((zerop gnus-tmp-level)
5145                (cond ((cdar thread)
5146                       (or gnus-sum-thread-tree-root subject))
5147                      (gnus-tmp-new-adopts
5148                       (or gnus-sum-thread-tree-false-root subject))
5149                      (t
5150                       (or gnus-sum-thread-tree-single-indent subject))))
5151               (t
5152                (concat (apply 'concat
5153                               (mapcar (lambda (item)
5154                                         (if (= item 1)
5155                                             gnus-sum-thread-tree-vertical
5156                                           gnus-sum-thread-tree-indent))
5157                                       (cdr (reverse tree-stack))))
5158                        (if (nth 1 thread)
5159                            gnus-sum-thread-tree-leaf-with-other
5160                          gnus-sum-thread-tree-single-leaf)))))
5161             (when (string= gnus-tmp-name "")
5162               (setq gnus-tmp-name gnus-tmp-from))
5163             (unless (numberp gnus-tmp-lines)
5164               (setq gnus-tmp-lines -1))
5165             (if (= gnus-tmp-lines -1)
5166                 (setq gnus-tmp-lines "?")
5167               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5168               (gnus-put-text-property
5169              (point)
5170              (progn (eval gnus-summary-line-format-spec) (point))
5171                'gnus-number number)
5172             (when gnus-visual-p
5173               (forward-line -1)
5174               (gnus-run-hooks 'gnus-summary-update-hook)
5175               (forward-line 1))
5176
5177             (setq gnus-tmp-prev-subject simp-subject)))
5178
5179         (when (nth 1 thread)
5180           (push (list (max 0 gnus-tmp-level)
5181                       (copy-sequence tree-stack)
5182                       (nthcdr 1 thread))
5183                 stack))
5184         (push (if (nth 1 thread) 1 0) tree-stack)
5185         (incf gnus-tmp-level)
5186         (setq threads (if thread-end nil (cdar thread)))
5187         (if gnus-summary-display-while-building
5188             (if building-count
5189                 (progn
5190                   ;; use a set frequency
5191                   (setq building-line-count (1- building-line-count))
5192                   (when (= building-line-count 0)
5193                     (sit-for 0)
5194                     (setq building-line-count
5195                           gnus-summary-display-while-building)))
5196               ;; always
5197               (sit-for 0)))
5198         (unless threads
5199           (setq gnus-tmp-level 0)))))
5200   (gnus-message 7 "Generating summary...done"))
5201
5202 (defun gnus-summary-prepare-unthreaded (headers)
5203   "Generate an unthreaded summary buffer based on HEADERS."
5204   (let (header number mark)
5205
5206     (beginning-of-line)
5207
5208     (while headers
5209       ;; We may have to root out some bad articles...
5210       (when (memq (setq number (mail-header-number
5211                                 (setq header (pop headers))))
5212                   gnus-newsgroup-limit)
5213         ;; Mark article as read when it has a low score.
5214         (when (and gnus-summary-mark-below
5215                    (< (or (cdr (assq number gnus-newsgroup-scored))
5216                           gnus-summary-default-score 0)
5217                       gnus-summary-mark-below)
5218                    (not (gnus-summary-article-ancient-p number)))
5219           (setq gnus-newsgroup-unreads
5220                 (delq number gnus-newsgroup-unreads))
5221           (if gnus-newsgroup-auto-expire
5222               (push number gnus-newsgroup-expirable)
5223             (push (cons number gnus-low-score-mark)
5224                   gnus-newsgroup-reads)))
5225
5226         (setq mark (gnus-article-mark number))
5227         (push (gnus-data-make number mark (1+ (point)) header 0)
5228               gnus-newsgroup-data)
5229         (gnus-summary-insert-line
5230          header 0 number
5231          (memq number gnus-newsgroup-undownloaded)
5232          mark (memq number gnus-newsgroup-replied)
5233          (memq number gnus-newsgroup-expirable)
5234          (mail-header-subject header) nil
5235          (cdr (assq number gnus-newsgroup-scored))
5236          (memq number gnus-newsgroup-processable))))))
5237
5238 (defun gnus-summary-remove-list-identifiers ()
5239   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5240   (let ((regexp (if (consp gnus-list-identifiers)
5241                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5242                   gnus-list-identifiers))
5243         changed subject)
5244     (when regexp
5245       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5246       (dolist (header gnus-newsgroup-headers)
5247         (setq subject (mail-header-subject header)
5248               changed nil)
5249         (while (string-match regexp subject)
5250           (setq subject
5251                 (concat (substring subject 0 (match-beginning 1))
5252                         (substring subject (match-end 0)))
5253                 changed t))
5254         (when changed
5255           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5256             (setq subject
5257                   (concat (substring subject 0 (match-beginning 1))
5258                           (substring subject (match-end 1)))))
5259           (mail-header-set-subject header subject))))))
5260
5261 (defun gnus-fetch-headers (articles)
5262   "Fetch headers of ARTICLES."
5263   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5264     (gnus-message 5 "Fetching headers for %s..." name)
5265     (prog1
5266         (if (eq 'nov
5267                 (setq gnus-headers-retrieved-by
5268                       (gnus-retrieve-headers
5269                        articles gnus-newsgroup-name
5270                        ;; We might want to fetch old headers, but
5271                        ;; not if there is only 1 article.
5272                        (and (or (and
5273                                  (not (eq gnus-fetch-old-headers 'some))
5274                                  (not (numberp gnus-fetch-old-headers)))
5275                                 (> (length articles) 1))
5276                             gnus-fetch-old-headers))))
5277             (gnus-get-newsgroup-headers-xover
5278              articles nil nil gnus-newsgroup-name t)
5279           (gnus-get-newsgroup-headers))
5280       (gnus-message 5 "Fetching headers for %s...done" name))))
5281
5282 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5283   "Select newsgroup GROUP.
5284 If READ-ALL is non-nil, all articles in the group are selected.
5285 If SELECT-ARTICLES, only select those articles from GROUP."
5286   (let* ((entry (gnus-group-entry group))
5287          ;;!!! Dirty hack; should be removed.
5288          (gnus-summary-ignore-duplicates
5289           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5290               t
5291             gnus-summary-ignore-duplicates))
5292          (info (nth 2 entry))
5293          articles fetched-articles cached)
5294
5295     (unless (gnus-check-server
5296              (set (make-local-variable 'gnus-current-select-method)
5297                   (gnus-find-method-for-group group)))
5298       (error "Couldn't open server"))
5299
5300     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5301         (gnus-activate-group group)     ; Or we can activate it...
5302         (progn                          ; Or we bug out.
5303           (when (equal major-mode 'gnus-summary-mode)
5304             (gnus-kill-buffer (current-buffer)))
5305           (error "Couldn't activate group %s: %s"
5306                  group (gnus-status-message group))))
5307
5308     (unless (gnus-request-group group t)
5309       (when (equal major-mode 'gnus-summary-mode)
5310         (gnus-kill-buffer (current-buffer)))
5311       (error "Couldn't request group %s: %s"
5312              group (gnus-status-message group)))
5313
5314     (when gnus-agent
5315       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5316
5317       (setq gnus-summary-use-undownloaded-faces
5318             (gnus-agent-find-parameter
5319              group
5320              'agent-enable-undownloaded-faces)))
5321
5322     (setq gnus-newsgroup-name group
5323           gnus-newsgroup-unselected nil
5324           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5325
5326     (let ((display (gnus-group-find-parameter group 'display)))
5327       (setq gnus-newsgroup-display
5328             (cond
5329              ((not (zerop (or (car-safe read-all) 0)))
5330               ;; The user entered the group with C-u SPC/RET, let's show
5331               ;; all articles.
5332               'gnus-not-ignore)
5333              ((eq display 'all)
5334               'gnus-not-ignore)
5335              ((arrayp display)
5336               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5337              ((numberp display)
5338               ;; The following is probably the "correct" solution, but
5339               ;; it makes Gnus fetch all headers and then limit the
5340               ;; articles (which is slow), so instead we hack the
5341               ;; select-articles parameter instead. -- Simon Josefsson
5342               ;; <jas@kth.se>
5343               ;;
5344               ;; (gnus-byte-compile
5345               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5346               ;;                         display)))))
5347               (setq select-articles
5348                     (gnus-uncompress-range
5349                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5350                              (if (> tmp 0)
5351                                  tmp
5352                                1))
5353                            (cdr (gnus-active group)))))
5354               nil)
5355              (t
5356               nil))))
5357
5358     (gnus-summary-setup-default-charset)
5359
5360     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5361     (when (gnus-virtual-group-p group)
5362       (setq cached gnus-newsgroup-cached))
5363
5364     (setq gnus-newsgroup-unreads
5365           (gnus-sorted-ndifference
5366            (gnus-sorted-ndifference gnus-newsgroup-unreads
5367                                     gnus-newsgroup-marked)
5368            gnus-newsgroup-dormant))
5369
5370     (setq gnus-newsgroup-processable nil)
5371
5372     (gnus-update-read-articles group gnus-newsgroup-unreads)
5373
5374     ;; Adjust and set lists of article marks.
5375     (when info
5376       (gnus-adjust-marked-articles info))
5377     (if (setq articles select-articles)
5378         (setq gnus-newsgroup-unselected
5379               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5380       (setq articles (gnus-articles-to-read group read-all)))
5381
5382     (cond
5383      ((null articles)
5384       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5385       'quit)
5386      ((eq articles 0) nil)
5387      (t
5388       ;; Init the dependencies hash table.
5389       (setq gnus-newsgroup-dependencies
5390             (gnus-make-hashtable (length articles)))
5391       (gnus-set-global-variables)
5392       ;; Retrieve the headers and read them in.
5393
5394       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5395
5396       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5397       (when cached
5398         (setq gnus-newsgroup-cached cached))
5399
5400       ;; Suppress duplicates?
5401       (when gnus-suppress-duplicates
5402         (gnus-dup-suppress-articles))
5403
5404       ;; Set the initial limit.
5405       (setq gnus-newsgroup-limit (copy-sequence articles))
5406       ;; Remove canceled articles from the list of unread articles.
5407       (setq fetched-articles
5408             (mapcar (lambda (headers) (mail-header-number headers))
5409                     gnus-newsgroup-headers))
5410       (setq gnus-newsgroup-articles fetched-articles)
5411       (setq gnus-newsgroup-unreads
5412             (gnus-sorted-nintersection
5413              gnus-newsgroup-unreads fetched-articles))
5414       (gnus-compute-unseen-list)
5415
5416       ;; Removed marked articles that do not exist.
5417       (gnus-update-missing-marks
5418        (gnus-sorted-difference articles fetched-articles))
5419       ;; We might want to build some more threads first.
5420       (when (and gnus-fetch-old-headers
5421                  (eq gnus-headers-retrieved-by 'nov))
5422         (if (eq gnus-fetch-old-headers 'invisible)
5423             (gnus-build-all-threads)
5424           (gnus-build-old-threads)))
5425       ;; Let the Gnus agent mark articles as read.
5426       (when gnus-agent
5427         (gnus-agent-get-undownloaded-list))
5428       ;; Remove list identifiers from subject
5429       (when gnus-list-identifiers
5430         (gnus-summary-remove-list-identifiers))
5431       ;; Check whether auto-expire is to be done in this group.
5432       (setq gnus-newsgroup-auto-expire
5433             (gnus-group-auto-expirable-p group))
5434       ;; Set up the article buffer now, if necessary.
5435       (unless gnus-single-article-buffer
5436         (gnus-article-setup-buffer))
5437       ;; First and last article in this newsgroup.
5438       (when gnus-newsgroup-headers
5439         (setq gnus-newsgroup-begin
5440               (mail-header-number (car gnus-newsgroup-headers))
5441               gnus-newsgroup-end
5442               (mail-header-number
5443                (gnus-last-element gnus-newsgroup-headers))))
5444       ;; GROUP is successfully selected.
5445       (or gnus-newsgroup-headers t)))))
5446
5447 (defun gnus-compute-unseen-list ()
5448   ;; The `seen' marks are treated specially.
5449   (if (not gnus-newsgroup-seen)
5450       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5451     (setq gnus-newsgroup-unseen
5452           (gnus-inverse-list-range-intersection
5453            gnus-newsgroup-articles gnus-newsgroup-seen))))
5454
5455 (defun gnus-summary-display-make-predicate (display)
5456   (require 'gnus-agent)
5457   (when (= (length display) 1)
5458     (setq display (car display)))
5459   (unless gnus-summary-display-cache
5460     (dolist (elem (append '((unread . unread)
5461                             (read . read)
5462                             (unseen . unseen))
5463                           gnus-article-mark-lists))
5464       (push (cons (cdr elem)
5465                   (gnus-byte-compile
5466                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5467             gnus-summary-display-cache)))
5468   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5469         (gnus-category-predicate-cache gnus-summary-display-cache))
5470     (gnus-get-predicate display)))
5471
5472 ;; Uses the dynamically bound `number' variable.
5473 (eval-when-compile
5474   (defvar number))
5475 (defun gnus-article-marked-p (type &optional article)
5476   (let ((article (or article number)))
5477     (cond
5478      ((eq type 'tick)
5479       (memq article gnus-newsgroup-marked))
5480      ((eq type 'spam)
5481       (memq article gnus-newsgroup-spam-marked))
5482      ((eq type 'unsend)
5483       (memq article gnus-newsgroup-unsendable))
5484      ((eq type 'undownload)
5485       (memq article gnus-newsgroup-undownloaded))
5486      ((eq type 'download)
5487       (memq article gnus-newsgroup-downloadable))
5488      ((eq type 'unread)
5489       (memq article gnus-newsgroup-unreads))
5490      ((eq type 'read)
5491       (memq article gnus-newsgroup-reads))
5492      ((eq type 'dormant)
5493       (memq article gnus-newsgroup-dormant) )
5494      ((eq type 'expire)
5495       (memq article gnus-newsgroup-expirable))
5496      ((eq type 'reply)
5497       (memq article gnus-newsgroup-replied))
5498      ((eq type 'killed)
5499       (memq article gnus-newsgroup-killed))
5500      ((eq type 'bookmark)
5501       (assq article gnus-newsgroup-bookmarks))
5502      ((eq type 'score)
5503       (assq article gnus-newsgroup-scored))
5504      ((eq type 'save)
5505       (memq article gnus-newsgroup-saved))
5506      ((eq type 'cache)
5507       (memq article gnus-newsgroup-cached))
5508      ((eq type 'forward)
5509       (memq article gnus-newsgroup-forwarded))
5510      ((eq type 'seen)
5511       (not (memq article gnus-newsgroup-unseen)))
5512      ((eq type 'recent)
5513       (memq article gnus-newsgroup-recent))
5514      (t t))))
5515
5516 (defun gnus-articles-to-read (group &optional read-all)
5517   "Find out what articles the user wants to read."
5518   (let* ((articles
5519           ;; Select all articles if `read-all' is non-nil, or if there
5520           ;; are no unread articles.
5521           (if (or read-all
5522                   (and (zerop (length gnus-newsgroup-marked))
5523                        (zerop (length gnus-newsgroup-unreads)))
5524                   ;; Fetch all if the predicate is non-nil.
5525                   gnus-newsgroup-display)
5526               ;; We want to select the headers for all the articles in
5527               ;; the group, so we select either all the active
5528               ;; articles in the group, or (if that's nil), the
5529               ;; articles in the cache.
5530               (or
5531                (gnus-uncompress-range (gnus-active group))
5532                (gnus-cache-articles-in-group group))
5533             ;; Select only the "normal" subset of articles.
5534             (gnus-sorted-nunion
5535              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5536              gnus-newsgroup-unreads)))
5537          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5538          (scored (length scored-list))
5539          (number (length articles))
5540          (marked (+ (length gnus-newsgroup-marked)
5541                     (length gnus-newsgroup-dormant)))
5542          (select
5543           (cond
5544            ((numberp read-all)
5545             read-all)
5546            ((numberp gnus-newsgroup-display)
5547             gnus-newsgroup-display)
5548            (t
5549             (condition-case ()
5550                 (cond
5551                  ((and (or (<= scored marked) (= scored number))
5552                        (numberp gnus-large-newsgroup)
5553                        (> number gnus-large-newsgroup))
5554                   (let* ((cursor-in-echo-area nil)
5555                          (initial (gnus-parameter-large-newsgroup-initial
5556                                    gnus-newsgroup-name))
5557                          (input
5558                           (read-string
5559                            (format
5560                             "How many articles from %s (%s %d): "
5561                             (gnus-limit-string
5562                              (gnus-group-decoded-name gnus-newsgroup-name)
5563                              35)
5564                             (if initial "max" "default")
5565                             number)
5566                            (if initial
5567                                (cons (number-to-string initial)
5568                                      0)))))
5569                     (if (string-match "^[ \t]*$" input) number input)))
5570                  ((and (> scored marked) (< scored number)
5571                        (> (- scored number) 20))
5572                   (let ((input
5573                          (read-string
5574                           (format "%s %s (%d scored, %d total): "
5575                                   "How many articles from"
5576                                   (gnus-group-decoded-name group)
5577                                   scored number))))
5578                     (if (string-match "^[ \t]*$" input)
5579                         number input)))
5580                  (t number))
5581               (quit
5582                (message "Quit getting the articles to read")
5583                nil))))))
5584     (setq select (if (stringp select) (string-to-number select) select))
5585     (if (or (null select) (zerop select))
5586         select
5587       (if (and (not (zerop scored)) (<= (abs select) scored))
5588           (progn
5589             (setq articles (sort scored-list '<))
5590             (setq number (length articles)))
5591         (setq articles (copy-sequence articles)))
5592
5593       (when (< (abs select) number)
5594         (if (< select 0)
5595             ;; Select the N oldest articles.
5596             (setcdr (nthcdr (1- (abs select)) articles) nil)
5597           ;; Select the N most recent articles.
5598           (setq articles (nthcdr (- number select) articles))))
5599       (setq gnus-newsgroup-unselected
5600             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5601       (when gnus-alter-articles-to-read-function
5602         (setq articles
5603               (sort
5604                (funcall gnus-alter-articles-to-read-function
5605                         gnus-newsgroup-name articles)
5606                '<)))
5607       articles)))
5608
5609 (defun gnus-killed-articles (killed articles)
5610   (let (out)
5611     (while articles
5612       (when (inline (gnus-member-of-range (car articles) killed))
5613         (push (car articles) out))
5614       (setq articles (cdr articles)))
5615     out))
5616
5617 (defun gnus-uncompress-marks (marks)
5618   "Uncompress the mark ranges in MARKS."
5619   (let ((uncompressed '(score bookmark))
5620         out)
5621     (while marks
5622       (if (memq (caar marks) uncompressed)
5623           (push (car marks) out)
5624         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5625       (setq marks (cdr marks)))
5626     out))
5627
5628 (defun gnus-article-mark-to-type (mark)
5629   "Return the type of MARK."
5630   (or (cadr (assq mark gnus-article-special-mark-lists))
5631       'list))
5632
5633 (defun gnus-article-unpropagatable-p (mark)
5634   "Return whether MARK should be propagated to back end."
5635   (memq mark gnus-article-unpropagated-mark-lists))
5636
5637 (defun gnus-adjust-marked-articles (info)
5638   "Set all article lists and remove all marks that are no longer valid."
5639   (let* ((marked-lists (gnus-info-marks info))
5640          (active (gnus-active (gnus-info-group info)))
5641          (min (car active))
5642          (max (cdr active))
5643          (types gnus-article-mark-lists)
5644          marks var articles article mark mark-type
5645          bgn end)
5646
5647     (dolist (marks marked-lists)
5648       (setq mark (car marks)
5649             mark-type (gnus-article-mark-to-type mark)
5650             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5651
5652       ;; We set the variable according to the type of the marks list,
5653       ;; and then adjust the marks to a subset of the active articles.
5654       (cond
5655        ;; Adjust "simple" lists - compressed yet unsorted
5656        ((eq mark-type 'list)
5657         ;; Simultaneously uncompress and clip to active range
5658         ;; See gnus-uncompress-range for a description of possible marks
5659         (let (l lh)
5660           (if (not (cadr marks))
5661               (set var nil)
5662             (setq articles (if (numberp (cddr marks))
5663                                (list (cdr marks))
5664                              (cdr marks))
5665                   lh (cons nil nil)
5666                   l lh)
5667
5668             (while (setq article (pop articles))
5669               (cond ((consp article)
5670                      (setq bgn (max (car article) min)
5671                            end (min (cdr article) max))
5672                      (while (<= bgn end)
5673                        (setq l (setcdr l (cons bgn nil))
5674                              bgn (1+ bgn))))
5675                     ((and (<= min article)
5676                           (>= max article))
5677                      (setq l (setcdr l (cons article nil))))))
5678             (set var (cdr lh)))))
5679        ;; Adjust assocs.
5680        ((eq mark-type 'tuple)
5681         (set var (setq articles (cdr marks)))
5682         (when (not (listp (cdr (symbol-value var))))
5683           (set var (list (symbol-value var))))
5684         (when (not (listp (cdr articles)))
5685           (setq articles (list articles)))
5686         (while articles
5687           (when (or (not (consp (setq article (pop articles))))
5688                     (< (car article) min)
5689                     (> (car article) max))
5690             (set var (delq article (symbol-value var))))))
5691        ;; Adjust ranges (sloppily).
5692        ((eq mark-type 'range)
5693         (cond
5694          ((eq mark 'seen)
5695           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5696           ;; It should be (seen (NUM1 . NUM2)).
5697           (when (numberp (cddr marks))
5698             (setcdr marks (list (cdr marks))))
5699           (setq articles (cdr marks))
5700           (while (and articles
5701                       (or (and (consp (car articles))
5702                                (> min (cdar articles)))
5703                           (and (numberp (car articles))
5704                                (> min (car articles)))))
5705             (pop articles))
5706           (set var articles))))))))
5707
5708 (defun gnus-update-missing-marks (missing)
5709   "Go through the list of MISSING articles and remove them from the mark lists."
5710   (when missing
5711     (let (var m)
5712       ;; Go through all types.
5713       (dolist (elem gnus-article-mark-lists)
5714         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5715           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5716           (when (symbol-value var)
5717             ;; This list has articles.  So we delete all missing
5718             ;; articles from it.
5719             (setq m missing)
5720             (while m
5721               (set var (delq (pop m) (symbol-value var))))))))))
5722
5723 (defun gnus-update-marks ()
5724   "Enter the various lists of marked articles into the newsgroup info list."
5725   (let ((types gnus-article-mark-lists)
5726         (info (gnus-get-info gnus-newsgroup-name))
5727         type list newmarked symbol delta-marks)
5728     (when info
5729       ;; Add all marks lists to the list of marks lists.
5730       (while (setq type (pop types))
5731         (setq list (symbol-value
5732                     (setq symbol
5733                           (intern (format "gnus-newsgroup-%s" (car type))))))
5734
5735         (when list
5736           ;; Get rid of the entries of the articles that have the
5737           ;; default score.
5738           (when (and (eq (cdr type) 'score)
5739                      gnus-save-score
5740                      list)
5741             (let* ((arts list)
5742                    (prev (cons nil list))
5743                    (all prev))
5744               (while arts
5745                 (if (or (not (consp (car arts)))
5746                         (= (cdar arts) gnus-summary-default-score))
5747                     (setcdr prev (cdr arts))
5748                   (setq prev arts))
5749                 (setq arts (cdr arts)))
5750               (setq list (cdr all)))))
5751
5752         (when (eq (cdr type) 'seen)
5753           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5754
5755         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5756           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5757
5758         (when (and (gnus-check-backend-function
5759                     'request-set-mark gnus-newsgroup-name)
5760                    (not (gnus-article-unpropagatable-p (cdr type))))
5761           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5762                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5763                  (add (gnus-remove-from-range
5764                        (gnus-copy-sequence list) old)))
5765             (when add
5766               (push (list add 'add (list (cdr type))) delta-marks))
5767             (when del
5768               (push (list del 'del (list (cdr type))) delta-marks))))
5769
5770         (when list
5771           (push (cons (cdr type) list) newmarked)))
5772
5773       (when delta-marks
5774         (unless (gnus-check-group gnus-newsgroup-name)
5775           (error "Can't open server for %s" gnus-newsgroup-name))
5776         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5777
5778       ;; Enter these new marks into the info of the group.
5779       (if (nthcdr 3 info)
5780           (setcar (nthcdr 3 info) newmarked)
5781         ;; Add the marks lists to the end of the info.
5782         (when newmarked
5783           (setcdr (nthcdr 2 info) (list newmarked))))
5784
5785       ;; Cut off the end of the info if there's nothing else there.
5786       (let ((i 5))
5787         (while (and (> i 2)
5788                     (not (nth i info)))
5789           (when (nthcdr (decf i) info)
5790             (setcdr (nthcdr i info) nil)))))))
5791
5792 (defun gnus-set-mode-line (where)
5793   "Set the mode line of the article or summary buffers.
5794 If WHERE is `summary', the summary mode line format will be used."
5795   ;; Is this mode line one we keep updated?
5796   (when (and (memq where gnus-updated-mode-lines)
5797              (symbol-value
5798               (intern (format "gnus-%s-mode-line-format-spec" where))))
5799     (let (mode-string)
5800       (save-excursion
5801         ;; We evaluate this in the summary buffer since these
5802         ;; variables are buffer-local to that buffer.
5803         (set-buffer gnus-summary-buffer)
5804        ;; We bind all these variables that are used in the `eval' form
5805         ;; below.
5806         (let* ((mformat (symbol-value
5807                          (intern
5808                           (format "gnus-%s-mode-line-format-spec" where))))
5809                (gnus-tmp-group-name (gnus-group-decoded-name
5810                                      gnus-newsgroup-name))
5811                (gnus-tmp-article-number (or gnus-current-article 0))
5812                (gnus-tmp-unread gnus-newsgroup-unreads)
5813                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5814                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5815                (gnus-tmp-unread-and-unselected
5816                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5817                             (zerop gnus-tmp-unselected))
5818                        "")
5819                       ((zerop gnus-tmp-unselected)
5820                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5821                       (t (format "{%d(+%d) more}"
5822                                  gnus-tmp-unread-and-unticked
5823                                  gnus-tmp-unselected))))
5824                (gnus-tmp-subject
5825                 (if (and gnus-current-headers
5826                          (vectorp gnus-current-headers))
5827                     (gnus-mode-string-quote
5828                      (mail-header-subject gnus-current-headers))
5829                   ""))
5830                bufname-length max-len
5831                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5832           (setq mode-string (eval mformat))
5833           (setq bufname-length (if (string-match "%b" mode-string)
5834                                    (- (length
5835                                        (buffer-name
5836                                         (if (eq where 'summary)
5837                                             nil
5838                                           (get-buffer gnus-article-buffer))))
5839                                       2)
5840                                  0))
5841           (setq max-len (max 4 (if gnus-mode-non-string-length
5842                                    (- (window-width)
5843                                       gnus-mode-non-string-length
5844                                       bufname-length)
5845                                  (length mode-string))))
5846           ;; We might have to chop a bit of the string off...
5847           (when (> (length mode-string) max-len)
5848             (setq mode-string
5849                   (concat (truncate-string-to-width mode-string (- max-len 3))
5850                           "...")))
5851           ;; Pad the mode string a bit.
5852           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5853       ;; Update the mode line.
5854       (setq mode-line-buffer-identification
5855             (gnus-mode-line-buffer-identification (list mode-string)))
5856       (set-buffer-modified-p t))))
5857
5858 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5859   "Go through the HEADERS list and add all Xrefs to a hash table.
5860 The resulting hash table is returned, or nil if no Xrefs were found."
5861   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5862          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5863          (xref-hashtb (gnus-make-hashtable))
5864          start group entry number xrefs header)
5865     (while headers
5866       (setq header (pop headers))
5867       (when (and (setq xrefs (mail-header-xref header))
5868                  (not (memq (setq number (mail-header-number header))
5869                             unreads)))
5870         (setq start 0)
5871         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5872           (setq start (match-end 0))
5873           (setq group (if prefix
5874                           (concat prefix (substring xrefs (match-beginning 1)
5875                                                     (match-end 1)))
5876                         (substring xrefs (match-beginning 1) (match-end 1))))
5877           (setq number
5878                 (string-to-number (substring xrefs (match-beginning 2)
5879                                           (match-end 2))))
5880           (if (setq entry (gnus-gethash group xref-hashtb))
5881               (setcdr entry (cons number (cdr entry)))
5882             (gnus-sethash group (cons number nil) xref-hashtb)))))
5883     (and start xref-hashtb)))
5884
5885 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5886   "Look through all the headers and mark the Xrefs as read."
5887   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5888         name info xref-hashtb idlist method nth4)
5889     (save-excursion
5890       (set-buffer gnus-group-buffer)
5891       (when (setq xref-hashtb
5892                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5893         (mapatoms
5894          (lambda (group)
5895            (unless (string= from-newsgroup (setq name (symbol-name group)))
5896              (setq idlist (symbol-value group))
5897              ;; Dead groups are not updated.
5898              (and (prog1
5899                       (setq info (gnus-get-info name))
5900                     (when (stringp (setq nth4 (gnus-info-method info)))
5901                       (setq nth4 (gnus-server-to-method nth4))))
5902                   ;; Only do the xrefs if the group has the same
5903                   ;; select method as the group we have just read.
5904                   (or (gnus-methods-equal-p
5905                        nth4 (gnus-find-method-for-group from-newsgroup))
5906                       virtual
5907                       (equal nth4 (setq method (gnus-find-method-for-group
5908                                                 from-newsgroup)))
5909                       (and (equal (car nth4) (car method))
5910                            (equal (nth 1 nth4) (nth 1 method))))
5911                   gnus-use-cross-reference
5912                   (or (not (eq gnus-use-cross-reference t))
5913                       virtual
5914                       ;; Only do cross-references on subscribed
5915                       ;; groups, if that is what is wanted.
5916                       (<= (gnus-info-level info) gnus-level-subscribed))
5917                   (gnus-group-make-articles-read name idlist))))
5918          xref-hashtb)))))
5919
5920 (defun gnus-compute-read-articles (group articles)
5921   (let* ((entry (gnus-group-entry group))
5922          (info (nth 2 entry))
5923          (active (gnus-active group))
5924          ninfo)
5925     (when entry
5926       ;; First peel off all invalid article numbers.
5927       (when active
5928         (let ((ids articles)
5929               id first)
5930           (while (setq id (pop ids))
5931             (when (and first (> id (cdr active)))
5932               ;; We'll end up in this situation in one particular
5933               ;; obscure situation.  If you re-scan a group and get
5934               ;; a new article that is cross-posted to a different
5935               ;; group that has not been re-scanned, you might get
5936               ;; crossposted article that has a higher number than
5937               ;; Gnus believes possible.  So we re-activate this
5938               ;; group as well.  This might mean doing the
5939               ;; crossposting thingy will *increase* the number
5940               ;; of articles in some groups.  Tsk, tsk.
5941               (setq active (or (gnus-activate-group group) active)))
5942             (when (or (> id (cdr active))
5943                       (< id (car active)))
5944               (setq articles (delq id articles))))))
5945       ;; If the read list is nil, we init it.
5946       (if (and active
5947                (null (gnus-info-read info))
5948                (> (car active) 1))
5949           (setq ninfo (cons 1 (1- (car active))))
5950         (setq ninfo (gnus-info-read info)))
5951       ;; Then we add the read articles to the range.
5952       (gnus-add-to-range
5953        ninfo (setq articles (sort articles '<))))))
5954
5955 (defun gnus-group-make-articles-read (group articles)
5956   "Update the info of GROUP to say that ARTICLES are read."
5957   (let* ((num 0)
5958          (entry (gnus-group-entry group))
5959          (info (nth 2 entry))
5960          (active (gnus-active group))
5961          range)
5962     (when entry
5963       (setq range (gnus-compute-read-articles group articles))
5964       (with-current-buffer gnus-group-buffer
5965         (gnus-undo-register
5966           `(progn
5967              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5968              (gnus-info-set-read ',info ',(gnus-info-read info))
5969              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5970              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5971              (gnus-group-update-group ,group t))))
5972       ;; Add the read articles to the range.
5973       (gnus-info-set-read info range)
5974       (gnus-request-set-mark group (list (list range 'add '(read))))
5975       ;; Then we have to re-compute how many unread
5976       ;; articles there are in this group.
5977       (when active
5978         (cond
5979          ((not range)
5980           (setq num (- (1+ (cdr active)) (car active))))
5981          ((not (listp (cdr range)))
5982           (setq num (- (cdr active) (- (1+ (cdr range))
5983                                        (car range)))))
5984          (t
5985           (while range
5986             (if (numberp (car range))
5987                 (setq num (1+ num))
5988               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5989             (setq range (cdr range)))
5990           (setq num (- (cdr active) num))))
5991         ;; Update the number of unread articles.
5992         (setcar entry num)
5993         ;; Update the group buffer.
5994         (unless (gnus-ephemeral-group-p group)
5995           (gnus-group-update-group group t))))))
5996
5997 (defvar gnus-newsgroup-none-id 0)
5998
5999 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6000   (let ((cur nntp-server-buffer)
6001         (dependencies
6002          (or dependencies
6003              (with-current-buffer gnus-summary-buffer
6004                gnus-newsgroup-dependencies)))
6005         headers id end ref number
6006         (mail-parse-charset gnus-newsgroup-charset)
6007         (mail-parse-ignored-charsets
6008          (save-excursion (condition-case nil
6009                              (set-buffer gnus-summary-buffer)
6010                            (error))
6011                          gnus-newsgroup-ignored-charsets)))
6012     (save-excursion
6013       (set-buffer nntp-server-buffer)
6014       ;; Translate all TAB characters into SPACE characters.
6015       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6016       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6017       (ietf-drums-unfold-fws)
6018       (gnus-run-hooks 'gnus-parse-headers-hook)
6019       (let ((case-fold-search t)
6020             in-reply-to header p lines chars)
6021         (goto-char (point-min))
6022         ;; Search to the beginning of the next header.  Error messages
6023         ;; do not begin with 2 or 3.
6024         (while (re-search-forward "^[23][0-9]+ " nil t)
6025           (setq id nil
6026                 ref nil)
6027           ;; This implementation of this function, with nine
6028           ;; search-forwards instead of the one re-search-forward and
6029           ;; a case (which basically was the old function) is actually
6030           ;; about twice as fast, even though it looks messier.  You
6031           ;; can't have everything, I guess.  Speed and elegance
6032           ;; doesn't always go hand in hand.
6033           (setq
6034            header
6035            (vector
6036             ;; Number.
6037             (prog1
6038                 (setq number (read cur))
6039               (end-of-line)
6040               (setq p (point))
6041               (narrow-to-region (point)
6042                                 (or (and (search-forward "\n.\n" nil t)
6043                                          (- (point) 2))
6044                                     (point))))
6045             ;; Subject.
6046             (progn
6047               (goto-char p)
6048               (if (search-forward "\nsubject:" nil t)
6049                   (funcall gnus-decode-encoded-word-function
6050                            (nnheader-header-value))
6051                 "(none)"))
6052             ;; From.
6053             (progn
6054               (goto-char p)
6055               (if (search-forward "\nfrom:" nil t)
6056                   (funcall gnus-decode-encoded-word-function
6057                            (nnheader-header-value))
6058                 "(nobody)"))
6059             ;; Date.
6060             (progn
6061               (goto-char p)
6062               (if (search-forward "\ndate:" nil t)
6063                   (nnheader-header-value) ""))
6064             ;; Message-ID.
6065             (progn
6066               (goto-char p)
6067               (setq id (if (re-search-forward
6068                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6069                            ;; We do it this way to make sure the Message-ID
6070                            ;; is (somewhat) syntactically valid.
6071                            (buffer-substring (match-beginning 1)
6072                                              (match-end 1))
6073                          ;; If there was no message-id, we just fake one
6074                          ;; to make subsequent routines simpler.
6075                          (nnheader-generate-fake-message-id number))))
6076             ;; References.
6077             (progn
6078               (goto-char p)
6079               (if (search-forward "\nreferences:" nil t)
6080                   (progn
6081                     (setq end (point))
6082                     (prog1
6083                         (nnheader-header-value)
6084                       (setq ref
6085                             (buffer-substring
6086                              (progn
6087                                (end-of-line)
6088                                (search-backward ">" end t)
6089                                (1+ (point)))
6090                              (progn
6091                                (search-backward "<" end t)
6092                                (point))))))
6093                 ;; Get the references from the in-reply-to header if there
6094                 ;; were no references and the in-reply-to header looks
6095                 ;; promising.
6096                 (if (and (search-forward "\nin-reply-to:" nil t)
6097                          (setq in-reply-to (nnheader-header-value))
6098                          (string-match "<[^>]+>" in-reply-to))
6099                     (let (ref2)
6100                       (setq ref (substring in-reply-to (match-beginning 0)
6101                                            (match-end 0)))
6102                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6103                         (setq ref2 (substring in-reply-to (match-beginning 0)
6104                                               (match-end 0)))
6105                         (when (> (length ref2) (length ref))
6106                           (setq ref ref2)))
6107                       ref)
6108                   (setq ref nil))))
6109             ;; Chars.
6110             (progn
6111               (goto-char p)
6112               (if (search-forward "\nchars: " nil t)
6113                   (if (numberp (setq chars (ignore-errors (read cur))))
6114                       chars -1)
6115                 -1))
6116             ;; Lines.
6117             (progn
6118               (goto-char p)
6119               (if (search-forward "\nlines: " nil t)
6120                   (if (numberp (setq lines (ignore-errors (read cur))))
6121                       lines -1)
6122                 -1))
6123             ;; Xref.
6124             (progn
6125               (goto-char p)
6126               (and (search-forward "\nxref:" nil t)
6127                    (nnheader-header-value)))
6128             ;; Extra.
6129             (when gnus-extra-headers
6130               (let ((extra gnus-extra-headers)
6131                     out)
6132                 (while extra
6133                   (goto-char p)
6134                   (when (search-forward
6135                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6136                     (push (cons (car extra) (nnheader-header-value))
6137                           out))
6138                   (pop extra))
6139                 out))))
6140           (when (equal id ref)
6141             (setq ref nil))
6142
6143           (when gnus-alter-header-function
6144             (funcall gnus-alter-header-function header)
6145             (setq id (mail-header-id header)
6146                   ref (gnus-parent-id (mail-header-references header))))
6147
6148           (when (setq header
6149                       (gnus-dependencies-add-header
6150                        header dependencies force-new))
6151             (push header headers))
6152           (goto-char (point-max))
6153           (widen))
6154         (nreverse headers)))))
6155
6156 ;; Goes through the xover lines and returns a list of vectors
6157 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6158                                                   force-new dependencies
6159                                                   group also-fetch-heads)
6160   "Parse the news overview data in the server buffer.
6161 Return a list of headers that match SEQUENCE (see
6162 `nntp-retrieve-headers')."
6163   ;; Get the Xref when the users reads the articles since most/some
6164   ;; NNTP servers do not include Xrefs when using XOVER.
6165   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6166   (let ((mail-parse-charset gnus-newsgroup-charset)
6167         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6168         (cur nntp-server-buffer)
6169         (dependencies (or dependencies gnus-newsgroup-dependencies))
6170         (allp (cond
6171                ((eq gnus-read-all-available-headers t)
6172                 t)
6173                ((stringp gnus-read-all-available-headers)
6174                 (string-match gnus-read-all-available-headers group))
6175                (t
6176                 nil)))
6177         number headers header)
6178     (save-excursion
6179       (set-buffer nntp-server-buffer)
6180       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6181       ;; Allow the user to mangle the headers before parsing them.
6182       (gnus-run-hooks 'gnus-parse-headers-hook)
6183       (goto-char (point-min))
6184       (gnus-parse-without-error
6185         (while (and (or sequence allp)
6186                     (not (eobp)))
6187           (setq number (read cur))
6188           (when (not allp)
6189             (while (and sequence
6190                         (< (car sequence) number))
6191               (setq sequence (cdr sequence))))
6192           (when (and (or allp
6193                          (and sequence
6194                               (eq number (car sequence))))
6195                      (progn
6196                        (setq sequence (cdr sequence))
6197                        (setq header (inline
6198                                       (gnus-nov-parse-line
6199                                        number dependencies force-new)))))
6200             (push header headers))
6201           (forward-line 1)))
6202       ;; A common bug in inn is that if you have posted an article and
6203       ;; then retrieves the active file, it will answer correctly --
6204       ;; the new article is included.  However, a NOV entry for the
6205       ;; article may not have been generated yet, so this may fail.
6206       ;; We work around this problem by retrieving the last few
6207       ;; headers using HEAD.
6208       (if (or (not also-fetch-heads)
6209               (not sequence))
6210           ;; We (probably) got all the headers.
6211           (nreverse headers)
6212         (let ((gnus-nov-is-evil t))
6213           (nconc
6214            (nreverse headers)
6215            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6216              (gnus-get-newsgroup-headers))))))))
6217
6218 (defun gnus-article-get-xrefs ()
6219   "Fill in the Xref value in `gnus-current-headers', if necessary.
6220 This is meant to be called in `gnus-article-internal-prepare-hook'."
6221   (let ((headers (with-current-buffer gnus-summary-buffer
6222                    gnus-current-headers)))
6223     (or (not gnus-use-cross-reference)
6224         (not headers)
6225         (and (mail-header-xref headers)
6226              (not (string= (mail-header-xref headers) "")))
6227         (let ((case-fold-search t)
6228               xref)
6229           (save-restriction
6230             (nnheader-narrow-to-headers)
6231             (goto-char (point-min))
6232             (when (or (and (not (eobp))
6233                            (eq (downcase (char-after)) ?x)
6234                            (looking-at "Xref:"))
6235                       (search-forward "\nXref:" nil t))
6236               (goto-char (1+ (match-end 0)))
6237               (setq xref (buffer-substring (point) (point-at-eol)))
6238               (mail-header-set-xref headers xref)))))))
6239
6240 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6241   "Find article ID and insert the summary line for that article.
6242 OLD-HEADER can either be a header or a line number to insert
6243 the subject line on."
6244   (let* ((line (and (numberp old-header) old-header))
6245          (old-header (and (vectorp old-header) old-header))
6246          (header (cond ((and old-header use-old-header)
6247                         old-header)
6248                        ((and (numberp id)
6249                              (gnus-number-to-header id))
6250                         (gnus-number-to-header id))
6251                        (t
6252                         (gnus-read-header id))))
6253          (number (and (numberp id) id))
6254          d)
6255     (when header
6256       ;; Rebuild the thread that this article is part of and go to the
6257       ;; article we have fetched.
6258       (when (and (not gnus-show-threads)
6259                  old-header)
6260         (when (and number
6261                    (setq d (gnus-data-find (mail-header-number old-header))))
6262           (goto-char (gnus-data-pos d))
6263           (gnus-data-remove
6264            number
6265            (- (point-at-bol)
6266               (prog1
6267                   (1+ (point-at-eol))
6268                 (gnus-delete-line))))))
6269       ;; Remove list identifiers from subject.
6270       (when gnus-list-identifiers
6271         (let ((gnus-newsgroup-headers (list header)))
6272           (gnus-summary-remove-list-identifiers)))
6273       (when old-header
6274         (mail-header-set-number header (mail-header-number old-header)))
6275       (setq gnus-newsgroup-sparse
6276             (delq (setq number (mail-header-number header))
6277                   gnus-newsgroup-sparse))
6278       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6279       (push number gnus-newsgroup-limit)
6280       (gnus-rebuild-thread (mail-header-id header) line)
6281       (gnus-summary-goto-subject number nil t))
6282     (when (and (numberp number)
6283                (> number 0))
6284       ;; We have to update the boundaries even if we can't fetch the
6285       ;; article if ID is a number -- so that the next `P' or `N'
6286       ;; command will fetch the previous (or next) article even
6287       ;; if the one we tried to fetch this time has been canceled.
6288       (when (> number gnus-newsgroup-end)
6289         (setq gnus-newsgroup-end number))
6290       (when (< number gnus-newsgroup-begin)
6291         (setq gnus-newsgroup-begin number))
6292       (setq gnus-newsgroup-unselected
6293             (delq number gnus-newsgroup-unselected)))
6294     ;; Report back a success?
6295     (and header (mail-header-number header))))
6296
6297 ;;; Process/prefix in the summary buffer
6298
6299 (defun gnus-summary-work-articles (n)
6300   "Return a list of articles to be worked upon.
6301 The prefix argument, the list of process marked articles, and the
6302 current article will be taken into consideration."
6303   (save-excursion
6304     (set-buffer gnus-summary-buffer)
6305     (cond
6306      (n
6307       ;; A numerical prefix has been given.
6308       (setq n (prefix-numeric-value n))
6309       (let ((backward (< n 0))
6310             (n (abs (prefix-numeric-value n)))
6311             articles article)
6312         (save-excursion
6313           (while
6314               (and (> n 0)
6315                    (push (setq article (gnus-summary-article-number))
6316                          articles)
6317                    (if backward
6318                        (gnus-summary-find-prev nil article)
6319                      (gnus-summary-find-next nil article)))
6320             (decf n)))
6321         (nreverse articles)))
6322      ((and (gnus-region-active-p) (mark))
6323       (message "region active")
6324       ;; Work on the region between point and mark.
6325       (let ((max (max (point) (mark)))
6326             articles article)
6327         (save-excursion
6328           (goto-char (min (point) (mark)))
6329           (while
6330               (and
6331                (push (setq article (gnus-summary-article-number)) articles)
6332                (gnus-summary-find-next nil article)
6333                (< (point) max)))
6334           (nreverse articles))))
6335      (gnus-newsgroup-processable
6336       ;; There are process-marked articles present.
6337       ;; Save current state.
6338       (gnus-summary-save-process-mark)
6339       ;; Return the list.
6340       (reverse gnus-newsgroup-processable))
6341      (t
6342       ;; Just return the current article.
6343       (list (gnus-summary-article-number))))))
6344
6345 (defmacro gnus-summary-iterate (arg &rest forms)
6346   "Iterate over the process/prefixed articles and do FORMS.
6347 ARG is the interactive prefix given to the command.  FORMS will be
6348 executed with point over the summary line of the articles."
6349   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6350     `(let ((,articles (gnus-summary-work-articles ,arg)))
6351        (while ,articles
6352          (gnus-summary-goto-subject (car ,articles))
6353          ,@forms
6354          (pop ,articles)))))
6355
6356 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6357 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6358
6359 (defun gnus-summary-save-process-mark ()
6360   "Push the current set of process marked articles on the stack."
6361   (interactive)
6362   (push (copy-sequence gnus-newsgroup-processable)
6363         gnus-newsgroup-process-stack))
6364
6365 (defun gnus-summary-kill-process-mark ()
6366   "Push the current set of process marked articles on the stack and unmark."
6367   (interactive)
6368   (gnus-summary-save-process-mark)
6369   (gnus-summary-unmark-all-processable))
6370
6371 (defun gnus-summary-yank-process-mark ()
6372   "Pop the last process mark state off the stack and restore it."
6373   (interactive)
6374   (unless gnus-newsgroup-process-stack
6375     (error "Empty mark stack"))
6376   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6377
6378 (defun gnus-summary-process-mark-set (set)
6379   "Make SET into the current process marked articles."
6380   (gnus-summary-unmark-all-processable)
6381   (mapc 'gnus-summary-set-process-mark set))
6382
6383 ;;; Searching and stuff
6384
6385 (defun gnus-summary-search-group (&optional backward use-level)
6386   "Search for next unread newsgroup.
6387 If optional argument BACKWARD is non-nil, search backward instead."
6388   (save-excursion
6389     (set-buffer gnus-group-buffer)
6390     (when (gnus-group-search-forward
6391            backward nil (if use-level (gnus-group-group-level) nil))
6392       (gnus-group-group-name))))
6393
6394 (defun gnus-summary-best-group (&optional exclude-group)
6395   "Find the name of the best unread group.
6396 If EXCLUDE-GROUP, do not go to this group."
6397   (with-current-buffer gnus-group-buffer
6398     (save-excursion
6399       (gnus-group-best-unread-group exclude-group))))
6400
6401 (defun gnus-summary-find-next (&optional unread article backward)
6402   (if backward
6403       (gnus-summary-find-prev unread article)
6404     (let* ((dummy (gnus-summary-article-intangible-p))
6405            (article (or article (gnus-summary-article-number)))
6406            (data (gnus-data-find-list article))
6407            result)
6408       (when (and (not dummy)
6409                  (or (not gnus-summary-check-current)
6410                      (not unread)
6411                      (not (gnus-data-unread-p (car data)))))
6412         (setq data (cdr data)))
6413       (when (setq result
6414                   (if unread
6415                       (progn
6416                         (while data
6417                           (unless (memq (gnus-data-number (car data))
6418                                         (cond
6419                                          ((eq gnus-auto-goto-ignores
6420                                               'always-undownloaded)
6421                                           gnus-newsgroup-undownloaded)
6422                                          (gnus-plugged
6423                                           nil)
6424                                          ((eq gnus-auto-goto-ignores
6425                                               'unfetched)
6426                                           gnus-newsgroup-unfetched)
6427                                          ((eq gnus-auto-goto-ignores
6428                                               'undownloaded)
6429                                           gnus-newsgroup-undownloaded)))
6430                             (when (gnus-data-unread-p (car data))
6431                               (setq result (car data)
6432                                     data nil)))
6433                           (setq data (cdr data)))
6434                         result)
6435                     (car data)))
6436         (goto-char (gnus-data-pos result))
6437         (gnus-data-number result)))))
6438
6439 (defun gnus-summary-find-prev (&optional unread article)
6440   (let* ((eobp (eobp))
6441          (article (or article (gnus-summary-article-number)))
6442          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6443          result)
6444     (when (and (not eobp)
6445                (or (not gnus-summary-check-current)
6446                    (not unread)
6447                    (not (gnus-data-unread-p (car data)))))
6448       (setq data (cdr data)))
6449     (when (setq result
6450                 (if unread
6451                     (progn
6452                       (while data
6453                         (unless (memq (gnus-data-number (car data))
6454                                       (cond
6455                                        ((eq gnus-auto-goto-ignores
6456                                             'always-undownloaded)
6457                                         gnus-newsgroup-undownloaded)
6458                                        (gnus-plugged
6459                                         nil)
6460                                        ((eq gnus-auto-goto-ignores
6461                                             'unfetched)
6462                                         gnus-newsgroup-unfetched)
6463                                        ((eq gnus-auto-goto-ignores
6464                                             'undownloaded)
6465                                         gnus-newsgroup-undownloaded)))
6466                           (when (gnus-data-unread-p (car data))
6467                             (setq result (car data)
6468                                   data nil)))
6469                         (setq data (cdr data)))
6470                       result)
6471                   (car data)))
6472       (goto-char (gnus-data-pos result))
6473       (gnus-data-number result))))
6474
6475 (defun gnus-summary-find-subject (subject &optional unread backward article)
6476   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6477          (article (or article (gnus-summary-article-number)))
6478          (articles (gnus-data-list backward))
6479          (arts (gnus-data-find-list article articles))
6480          result)
6481     (when (or (not gnus-summary-check-current)
6482               (not unread)
6483               (not (gnus-data-unread-p (car arts))))
6484       (setq arts (cdr arts)))
6485     (while arts
6486       (and (or (not unread)
6487                (gnus-data-unread-p (car arts)))
6488            (vectorp (gnus-data-header (car arts)))
6489            (gnus-subject-equal
6490             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6491            (setq result (car arts)
6492                  arts nil))
6493       (setq arts (cdr arts)))
6494     (and result
6495          (goto-char (gnus-data-pos result))
6496          (gnus-data-number result))))
6497
6498 (defun gnus-summary-search-forward (&optional unread subject backward)
6499   "Search forward for an article.
6500 If UNREAD, look for unread articles.  If SUBJECT, look for
6501 articles with that subject.  If BACKWARD, search backward instead."
6502   (cond (subject (gnus-summary-find-subject subject unread backward))
6503         (backward (gnus-summary-find-prev unread))
6504         (t (gnus-summary-find-next unread))))
6505
6506 (defun gnus-recenter (&optional n)
6507   "Center point in window and redisplay frame.
6508 Also do horizontal recentering."
6509   (interactive "P")
6510   (when (and gnus-auto-center-summary
6511              (not (eq gnus-auto-center-summary 'vertical)))
6512     (gnus-horizontal-recenter))
6513   (recenter n))
6514
6515 (defun gnus-summary-recenter ()
6516   "Center point in the summary window.
6517 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6518 displayed, no centering will be performed."
6519   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6520   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6521   (interactive)
6522   ;; The user has to want it.
6523   (when gnus-auto-center-summary
6524     (let* ((top (cond ((< (window-height) 4) 0)
6525                       ((< (window-height) 7) 1)
6526                       (t (if (numberp gnus-auto-center-summary)
6527                              gnus-auto-center-summary
6528                            (/ (1- (window-height)) 2)))))
6529            (height (1- (window-height)))
6530            (bottom (save-excursion (goto-char (point-max))
6531                                    (forward-line (- height))
6532                                    (point)))
6533            (window (get-buffer-window (current-buffer))))
6534       (when (get-buffer-window gnus-article-buffer)
6535         ;; Only do recentering when the article buffer is displayed,
6536         ;; Set the window start to either `bottom', which is the biggest
6537         ;; possible valid number, or the second line from the top,
6538         ;; whichever is the least.
6539         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6540           (if (> bottom top-pos)
6541               ;; Keep the second line from the top visible
6542               (set-window-start window top-pos t)
6543             ;; Try to keep the bottom line visible; if it's partially
6544             ;; obscured, either scroll one more line to make it fully
6545             ;; visible, or revert to using TOP-POS.
6546             (save-excursion
6547               (goto-char (point-max))
6548               (forward-line -1)
6549               (let ((last-line-start (point)))
6550                 (goto-char bottom)
6551                 (set-window-start window (point) t)
6552                 (when (not (pos-visible-in-window-p last-line-start window))
6553                   (forward-line 1)
6554                   (set-window-start window (min (point) top-pos) t)))))))
6555       ;; Do horizontal recentering while we're at it.
6556       (when (and (get-buffer-window (current-buffer) t)
6557                  (not (eq gnus-auto-center-summary 'vertical)))
6558         (let ((selected (selected-window)))
6559           (select-window (get-buffer-window (current-buffer) t))
6560           (gnus-summary-position-point)
6561           (gnus-horizontal-recenter)
6562           (select-window selected))))))
6563
6564 (defun gnus-summary-jump-to-group (newsgroup)
6565   "Move point to NEWSGROUP in group mode buffer."
6566   ;; Keep update point of group mode buffer if visible.
6567   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6568       (save-window-excursion
6569         ;; Take care of tree window mode.
6570         (when (get-buffer-window gnus-group-buffer)
6571           (pop-to-buffer gnus-group-buffer))
6572         (gnus-group-jump-to-group newsgroup))
6573     (save-excursion
6574       ;; Take care of tree window mode.
6575       (if (get-buffer-window gnus-group-buffer)
6576           (pop-to-buffer gnus-group-buffer)
6577         (set-buffer gnus-group-buffer))
6578       (gnus-group-jump-to-group newsgroup))))
6579
6580 ;; This function returns a list of article numbers based on the
6581 ;; difference between the ranges of read articles in this group and
6582 ;; the range of active articles.
6583 (defun gnus-list-of-unread-articles (group)
6584   (let* ((read (gnus-info-read (gnus-get-info group)))
6585          (active (or (gnus-active group) (gnus-activate-group group)))
6586          (last (or (cdr active)
6587                    (error "Group %s couldn't be activated " group)))
6588          first nlast unread)
6589     ;; If none are read, then all are unread.
6590     (if (not read)
6591         (setq first (car active))
6592       ;; If the range of read articles is a single range, then the
6593       ;; first unread article is the article after the last read
6594       ;; article.  Sounds logical, doesn't it?
6595       (if (and (not (listp (cdr read)))
6596                (or (< (car read) (car active))
6597                    (progn (setq read (list read))
6598                           nil)))
6599           (setq first (max (car active) (1+ (cdr read))))
6600         ;; `read' is a list of ranges.
6601         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6602                                   (caar read)))
6603                   1)
6604           (setq first (car active)))
6605         (while read
6606           (when first
6607             (while (< first nlast)
6608               (setq unread (cons first unread)
6609                     first (1+ first))))
6610           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6611           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6612           (setq read (cdr read)))))
6613     ;; And add the last unread articles.
6614     (while (<= first last)
6615       (setq unread (cons first unread)
6616             first (1+ first)))
6617     ;; Return the list of unread articles.
6618     (delq 0 (nreverse unread))))
6619
6620 (defun gnus-list-of-read-articles (group)
6621   "Return a list of unread, unticked and non-dormant articles."
6622   (let* ((info (gnus-get-info group))
6623          (marked (gnus-info-marks info))
6624          (active (gnus-active group)))
6625     (and info active
6626          (gnus-list-range-difference
6627           (gnus-list-range-difference
6628            (gnus-sorted-complement
6629             (gnus-uncompress-range active)
6630             (gnus-list-of-unread-articles group))
6631            (cdr (assq 'dormant marked)))
6632           (cdr (assq 'tick marked))))))
6633
6634 ;; This function returns a sequence of article numbers based on the
6635 ;; difference between the ranges of read articles in this group and
6636 ;; the range of active articles.
6637 (defun gnus-sequence-of-unread-articles (group)
6638   (let* ((read (gnus-info-read (gnus-get-info group)))
6639          (active (or (gnus-active group) (gnus-activate-group group)))
6640          (last (cdr active))
6641          first nlast unread)
6642     ;; If none are read, then all are unread.
6643     (if (not read)
6644         (setq first (car active))
6645       ;; If the range of read articles is a single range, then the
6646       ;; first unread article is the article after the last read
6647       ;; article.  Sounds logical, doesn't it?
6648       (if (and (not (listp (cdr read)))
6649                (or (< (car read) (car active))
6650                    (progn (setq read (list read))
6651                           nil)))
6652           (setq first (max (car active) (1+ (cdr read))))
6653         ;; `read' is a list of ranges.
6654         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6655                                   (caar read)))
6656                   1)
6657           (setq first (car active)))
6658         (while read
6659           (when first
6660             (push (cons first nlast) unread))
6661           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6662           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6663           (setq read (cdr read)))))
6664     ;; And add the last unread articles.
6665     (cond ((not (and first last))
6666            nil)
6667           ((< first last)
6668            (push (cons first last) unread))
6669           ((= first last)
6670            (push first unread)))
6671     ;; Return the sequence of unread articles.
6672     (delq 0 (nreverse unread))))
6673
6674 ;; Various summary commands
6675
6676 (defun gnus-summary-select-article-buffer ()
6677   "Reconfigure windows to show article buffer."
6678   (interactive)
6679   (if (not (gnus-buffer-live-p gnus-article-buffer))
6680       (error "There is no article buffer for this summary buffer")
6681     (gnus-configure-windows 'article)
6682     (select-window (get-buffer-window gnus-article-buffer))))
6683
6684 (defun gnus-summary-universal-argument (arg)
6685   "Perform any operation on all articles that are process/prefixed."
6686   (interactive "P")
6687   (let ((articles (gnus-summary-work-articles arg))
6688         func article)
6689     (if (eq
6690          (setq
6691           func
6692           (key-binding
6693            (read-key-sequence
6694             (substitute-command-keys
6695              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6696          'undefined)
6697         (gnus-error 1 "Undefined key")
6698       (save-excursion
6699         (while articles
6700           (gnus-summary-goto-subject (setq article (pop articles)))
6701           (let (gnus-newsgroup-processable)
6702             (command-execute func))
6703           (gnus-summary-remove-process-mark article)))))
6704   (gnus-summary-position-point))
6705
6706 (defun gnus-summary-toggle-truncation (&optional arg)
6707   "Toggle truncation of summary lines.
6708 With ARG, turn line truncation on if ARG is positive."
6709   (interactive "P")
6710   (setq truncate-lines
6711         (if (null arg) (not truncate-lines)
6712           (> (prefix-numeric-value arg) 0)))
6713   (redraw-display))
6714
6715 (defun gnus-summary-find-for-reselect ()
6716   "Return the number of an article to stay on across a reselect.
6717 The current article is considered, then following articles, then previous
6718 articles.  An article is sought which is not cancelled and isn't a temporary
6719 insertion from another group.  If there's no such then return a dummy 0."
6720   (let (found)
6721     (dolist (rev '(nil t))
6722       (unless found      ; don't demand the reverse list if we don't need it
6723         (let ((data (gnus-data-find-list
6724                      (gnus-summary-article-number) (gnus-data-list rev))))
6725           (while (and data (not found))
6726             (if (and (< 0 (gnus-data-number (car data)))
6727                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6728                 (setq found (gnus-data-number (car data))))
6729             (setq data (cdr data))))))
6730     (or found 0)))
6731
6732 (defun gnus-summary-reselect-current-group (&optional all rescan)
6733   "Exit and then reselect the current newsgroup.
6734 The prefix argument ALL means to select all articles."
6735   (interactive "P")
6736   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6737     (error "Ephemeral groups can't be reselected"))
6738   (let ((current-subject (gnus-summary-find-for-reselect))
6739         (group gnus-newsgroup-name))
6740     (setq gnus-newsgroup-begin nil)
6741     (gnus-summary-exit nil 'leave-hidden)
6742     ;; We have to adjust the point of group mode buffer because
6743     ;; point was moved to the next unread newsgroup by exiting.
6744     (gnus-summary-jump-to-group group)
6745     (when rescan
6746       (save-excursion
6747         (gnus-group-get-new-news-this-group 1)))
6748     (gnus-group-read-group all t)
6749     (gnus-summary-goto-subject current-subject nil t)))
6750
6751 (defun gnus-summary-rescan-group (&optional all)
6752   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6753   (interactive "P")
6754   (gnus-summary-reselect-current-group all t))
6755
6756 (defun gnus-summary-update-info (&optional non-destructive)
6757   (save-excursion
6758     (let ((group gnus-newsgroup-name))
6759       (when group
6760         (when gnus-newsgroup-kill-headers
6761           (setq gnus-newsgroup-killed
6762                 (gnus-compress-sequence
6763                  (gnus-sorted-union
6764                   (gnus-list-range-intersection
6765                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6766                   gnus-newsgroup-unreads)
6767                  t)))
6768         (unless (listp (cdr gnus-newsgroup-killed))
6769           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6770         (let ((headers gnus-newsgroup-headers))
6771           ;; Set the new ranges of read articles.
6772           (with-current-buffer gnus-group-buffer
6773             (gnus-undo-force-boundary))
6774           (gnus-update-read-articles
6775            group (gnus-sorted-union
6776                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6777           ;; Set the current article marks.
6778           (let ((gnus-newsgroup-scored
6779                  (if (and (not gnus-save-score)
6780                           (not non-destructive))
6781                      nil
6782                    gnus-newsgroup-scored)))
6783             (save-excursion
6784               (gnus-update-marks)))
6785           ;; Do the cross-ref thing.
6786           (when gnus-use-cross-reference
6787             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6788           ;; Do not switch windows but change the buffer to work.
6789           (set-buffer gnus-group-buffer)
6790           (unless (gnus-ephemeral-group-p group)
6791             (gnus-group-update-group group)))))))
6792
6793 (defun gnus-summary-save-newsrc (&optional force)
6794   "Save the current number of read/marked articles in the dribble buffer.
6795 The dribble buffer will then be saved.
6796 If FORCE (the prefix), also save the .newsrc file(s)."
6797   (interactive "P")
6798   (gnus-summary-update-info t)
6799   (if force
6800       (gnus-save-newsrc-file)
6801     (gnus-dribble-save)))
6802
6803 (defun gnus-summary-exit (&optional temporary leave-hidden)
6804   "Exit reading current newsgroup, and then return to group selection mode.
6805 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6806   (interactive)
6807   (gnus-set-global-variables)
6808   (when (gnus-buffer-live-p gnus-article-buffer)
6809     (save-excursion
6810       (set-buffer gnus-article-buffer)
6811       (mm-destroy-parts gnus-article-mime-handles)
6812       ;; Set it to nil for safety reason.
6813       (setq gnus-article-mime-handle-alist nil)
6814       (setq gnus-article-mime-handles nil)))
6815   (gnus-kill-save-kill-buffer)
6816   (gnus-async-halt-prefetch)
6817   (let* ((group gnus-newsgroup-name)
6818          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6819          (gnus-group-is-exiting-p t)
6820          (mode major-mode)
6821          (group-point nil)
6822          (buf (current-buffer)))
6823     (unless quit-config
6824       ;; Do adaptive scoring, and possibly save score files.
6825       (when gnus-newsgroup-adaptive
6826         (gnus-score-adaptive))
6827       (when gnus-use-scoring
6828         (gnus-score-save)))
6829     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6830     ;; If we have several article buffers, we kill them at exit.
6831     (unless gnus-single-article-buffer
6832       (gnus-kill-buffer gnus-original-article-buffer)
6833       (setq gnus-article-current nil))
6834     (when gnus-use-cache
6835       (gnus-cache-possibly-remove-articles)
6836       (gnus-cache-save-buffers))
6837     (gnus-async-prefetch-remove-group group)
6838     (when gnus-suppress-duplicates
6839       (gnus-dup-enter-articles))
6840     (when gnus-use-trees
6841       (gnus-tree-close group))
6842     (when gnus-use-cache
6843       (gnus-cache-write-active))
6844     ;; Remove entries for this group.
6845     (nnmail-purge-split-history (gnus-group-real-name group))
6846     ;; Make all changes in this group permanent.
6847     (unless quit-config
6848       (gnus-run-hooks 'gnus-exit-group-hook)
6849       (gnus-summary-update-info))
6850     (gnus-close-group group)
6851     ;; Make sure where we were, and go to next newsgroup.
6852     (set-buffer gnus-group-buffer)
6853     (unless quit-config
6854       (gnus-group-jump-to-group group))
6855     (gnus-run-hooks 'gnus-summary-exit-hook)
6856     (unless (or quit-config
6857                 ;; If this group has disappeared from the summary
6858                 ;; buffer, don't skip forwards.
6859                 (not (string= group (gnus-group-group-name))))
6860       (gnus-group-next-unread-group 1))
6861     (setq group-point (point))
6862     (if temporary
6863         nil                             ;Nothing to do.
6864       ;; If we have several article buffers, we kill them at exit.
6865       (unless gnus-single-article-buffer
6866         (gnus-kill-buffer gnus-article-buffer)
6867         (gnus-kill-buffer gnus-original-article-buffer)
6868         (setq gnus-article-current nil))
6869       (set-buffer buf)
6870       (if (not gnus-kill-summary-on-exit)
6871           (progn
6872             (gnus-deaden-summary)
6873             (setq mode nil))
6874         ;; We set all buffer-local variables to nil.  It is unclear why
6875         ;; this is needed, but if we don't, buffer-local variables are
6876         ;; not garbage-collected, it seems.  This would the lead to en
6877         ;; ever-growing Emacs.
6878         (gnus-summary-clear-local-variables)
6879         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6880           (gnus-summary-clear-local-variables))
6881         (when (get-buffer gnus-article-buffer)
6882           (bury-buffer gnus-article-buffer))
6883         ;; Return to group mode buffer.
6884         (when (eq mode 'gnus-summary-mode)
6885           (gnus-kill-buffer buf)))
6886       (setq gnus-current-select-method gnus-select-method)
6887       (set-buffer gnus-group-buffer)
6888       (if quit-config
6889           (gnus-handle-ephemeral-exit quit-config)
6890         (goto-char group-point)
6891         ;; If gnus-group-buffer is already displayed, make sure we also move
6892         ;; the cursor in the window that displays it.
6893         (let ((win (get-buffer-window (current-buffer) 0)))
6894           (if win (set-window-point win (point))))
6895         (unless leave-hidden
6896           (gnus-configure-windows 'group 'force)))
6897       ;; Clear the current group name.
6898       (unless quit-config
6899         (setq gnus-newsgroup-name nil)))))
6900
6901 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6902 (defun gnus-summary-exit-no-update (&optional no-questions)
6903   "Quit reading current newsgroup without updating read article info."
6904   (interactive)
6905   (let* ((group gnus-newsgroup-name)
6906          (gnus-group-is-exiting-p t)
6907          (gnus-group-is-exiting-without-update-p t)
6908          (quit-config (gnus-group-quit-config group)))
6909     (when (or no-questions
6910               gnus-expert-user
6911               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6912       (gnus-async-halt-prefetch)
6913       (run-hooks 'gnus-summary-prepare-exit-hook)
6914       (when (gnus-buffer-live-p gnus-article-buffer)
6915         (save-excursion
6916           (set-buffer gnus-article-buffer)
6917           (mm-destroy-parts gnus-article-mime-handles)
6918           ;; Set it to nil for safety reason.
6919           (setq gnus-article-mime-handle-alist nil)
6920           (setq gnus-article-mime-handles nil)))
6921       ;; If we have several article buffers, we kill them at exit.
6922       (unless gnus-single-article-buffer
6923         (gnus-kill-buffer gnus-article-buffer)
6924         (gnus-kill-buffer gnus-original-article-buffer)
6925         (setq gnus-article-current nil))
6926       (if (not gnus-kill-summary-on-exit)
6927           (gnus-deaden-summary)
6928         (gnus-close-group group)
6929         (gnus-summary-clear-local-variables)
6930         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6931           (gnus-summary-clear-local-variables))
6932         (gnus-kill-buffer gnus-summary-buffer))
6933       (unless gnus-single-article-buffer
6934         (setq gnus-article-current nil))
6935       (when gnus-use-trees
6936         (gnus-tree-close group))
6937       (gnus-async-prefetch-remove-group group)
6938       (when (get-buffer gnus-article-buffer)
6939         (bury-buffer gnus-article-buffer))
6940       ;; Return to the group buffer.
6941       (gnus-configure-windows 'group 'force)
6942       ;; Clear the current group name.
6943       (setq gnus-newsgroup-name nil)
6944       (unless (gnus-ephemeral-group-p group)
6945         (gnus-group-update-group group))
6946       (when (equal (gnus-group-group-name) group)
6947         (gnus-group-next-unread-group 1))
6948       (when quit-config
6949         (gnus-handle-ephemeral-exit quit-config)))))
6950
6951 (defun gnus-handle-ephemeral-exit (quit-config)
6952   "Handle movement when leaving an ephemeral group.
6953 The state which existed when entering the ephemeral is reset."
6954   (if (not (buffer-name (car quit-config)))
6955       (gnus-configure-windows 'group 'force)
6956     (set-buffer (car quit-config))
6957     (cond ((eq major-mode 'gnus-summary-mode)
6958            (gnus-set-global-variables))
6959           ((eq major-mode 'gnus-article-mode)
6960            (save-excursion
6961              ;; The `gnus-summary-buffer' variable may point
6962              ;; to the old summary buffer when using a single
6963              ;; article buffer.
6964              (unless (gnus-buffer-live-p gnus-summary-buffer)
6965                (set-buffer gnus-group-buffer))
6966              (set-buffer gnus-summary-buffer)
6967              (gnus-set-global-variables))))
6968     (if (or (eq (cdr quit-config) 'article)
6969             (eq (cdr quit-config) 'pick))
6970         (progn
6971           ;; The current article may be from the ephemeral group
6972           ;; thus it is best that we reload this article
6973           ;;
6974           ;; If we're exiting from a large digest, this can be
6975           ;; extremely slow.  So, it's better not to reload it. -- jh.
6976           ;;(gnus-summary-show-article)
6977           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6978               (gnus-configure-windows 'pick 'force)
6979             (gnus-configure-windows (cdr quit-config) 'force)))
6980       (gnus-configure-windows (cdr quit-config) 'force))
6981     (when (eq major-mode 'gnus-summary-mode)
6982       (gnus-summary-next-subject 1 nil t)
6983       (gnus-summary-recenter)
6984       (gnus-summary-position-point))))
6985
6986 ;;; Dead summaries.
6987
6988 (defvar gnus-dead-summary-mode-map nil)
6989
6990 (unless gnus-dead-summary-mode-map
6991   (setq gnus-dead-summary-mode-map (make-keymap))
6992   (suppress-keymap gnus-dead-summary-mode-map)
6993   (substitute-key-definition
6994    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6995   (dolist (key '("\C-d" "\r" "\177" [delete]))
6996     (define-key gnus-dead-summary-mode-map
6997       key 'gnus-summary-wake-up-the-dead))
6998   (dolist (key '("q" "Q"))
6999     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
7000
7001 (defvar gnus-dead-summary-mode nil
7002   "Minor mode for Gnus summary buffers.")
7003
7004 (defun gnus-dead-summary-mode (&optional arg)
7005   "Minor mode for Gnus summary buffers."
7006   (interactive "P")
7007   (when (eq major-mode 'gnus-summary-mode)
7008     (make-local-variable 'gnus-dead-summary-mode)
7009     (setq gnus-dead-summary-mode
7010           (if (null arg) (not gnus-dead-summary-mode)
7011             (> (prefix-numeric-value arg) 0)))
7012     (when gnus-dead-summary-mode
7013       (add-minor-mode
7014        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
7015
7016 (defun gnus-deaden-summary ()
7017   "Make the current summary buffer into a dead summary buffer."
7018   ;; Kill any previous dead summary buffer.
7019   (when (and gnus-dead-summary
7020              (buffer-name gnus-dead-summary))
7021     (with-current-buffer gnus-dead-summary
7022       (when gnus-dead-summary-mode
7023         (kill-buffer (current-buffer)))))
7024   ;; Make this the current dead summary.
7025   (setq gnus-dead-summary (current-buffer))
7026   (gnus-dead-summary-mode 1)
7027   (let ((name (buffer-name)))
7028     (when (string-match "Summary" name)
7029       (rename-buffer
7030        (concat (substring name 0 (match-beginning 0)) "Dead "
7031                (substring name (match-beginning 0)))
7032        t)
7033       (bury-buffer))))
7034
7035 (defun gnus-kill-or-deaden-summary (buffer)
7036   "Kill or deaden the summary BUFFER."
7037   (save-excursion
7038     (when (and (buffer-name buffer)
7039                (not gnus-single-article-buffer))
7040       (with-current-buffer buffer
7041         (gnus-kill-buffer gnus-article-buffer)
7042         (gnus-kill-buffer gnus-original-article-buffer)))
7043     (cond
7044      ;; Kill the buffer.
7045      (gnus-kill-summary-on-exit
7046       (when (and gnus-use-trees
7047                  (gnus-buffer-exists-p buffer))
7048         (save-excursion
7049           (set-buffer buffer)
7050           (gnus-tree-close gnus-newsgroup-name)))
7051       (gnus-kill-buffer buffer))
7052      ;; Deaden the buffer.
7053      ((gnus-buffer-exists-p buffer)
7054       (save-excursion
7055         (set-buffer buffer)
7056         (gnus-deaden-summary))))))
7057
7058 (defun gnus-summary-wake-up-the-dead (&rest args)
7059   "Wake up the dead summary buffer."
7060   (interactive)
7061   (gnus-dead-summary-mode -1)
7062   (let ((name (buffer-name)))
7063     (when (string-match "Dead " name)
7064       (rename-buffer
7065        (concat (substring name 0 (match-beginning 0))
7066                (substring name (match-end 0)))
7067        t)))
7068   (gnus-message 3 "This dead summary is now alive again"))
7069
7070 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7071 (defun gnus-summary-fetch-faq (&optional faq-dir)
7072   "Fetch the FAQ for the current group.
7073 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
7074 in."
7075   (interactive
7076    (list
7077     (when current-prefix-arg
7078       (completing-read
7079        "FAQ dir: " (and (listp gnus-group-faq-directory)
7080                         (mapcar 'list
7081                                 gnus-group-faq-directory))))))
7082   (let (gnus-faq-buffer)
7083     (when (setq gnus-faq-buffer
7084                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
7085       (gnus-configure-windows 'summary-faq))))
7086
7087 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7088 (defun gnus-summary-describe-group (&optional force)
7089   "Describe the current newsgroup."
7090   (interactive "P")
7091   (gnus-group-describe-group force gnus-newsgroup-name))
7092
7093 (defun gnus-summary-describe-briefly ()
7094   "Describe summary mode commands briefly."
7095   (interactive)
7096   (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")))
7097
7098 ;; Walking around group mode buffer from summary mode.
7099
7100 (defun gnus-summary-next-group (&optional no-article target-group backward)
7101   "Exit current newsgroup and then select next unread newsgroup.
7102 If prefix argument NO-ARTICLE is non-nil, no article is selected
7103 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7104 previous group instead."
7105   (interactive "P")
7106   ;; Stop pre-fetching.
7107   (gnus-async-halt-prefetch)
7108   (let ((current-group gnus-newsgroup-name)
7109         (current-buffer (current-buffer))
7110         entered)
7111     ;; First we semi-exit this group to update Xrefs and all variables.
7112     ;; We can't do a real exit, because the window conf must remain
7113     ;; the same in case the user is prompted for info, and we don't
7114     ;; want the window conf to change before that...
7115     (gnus-summary-exit t)
7116     (while (not entered)
7117       ;; Then we find what group we are supposed to enter.
7118       (set-buffer gnus-group-buffer)
7119       (gnus-group-jump-to-group current-group)
7120       (setq target-group
7121             (or target-group
7122                 (if (eq gnus-keep-same-level 'best)
7123                     (gnus-summary-best-group gnus-newsgroup-name)
7124                   (gnus-summary-search-group backward gnus-keep-same-level))))
7125       (if (not target-group)
7126           ;; There are no further groups, so we return to the group
7127           ;; buffer.
7128           (progn
7129             (gnus-message 5 "Returning to the group buffer")
7130             (setq entered t)
7131             (when (gnus-buffer-live-p current-buffer)
7132               (set-buffer current-buffer)
7133               (gnus-summary-exit))
7134             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7135         ;; We try to enter the target group.
7136         (gnus-group-jump-to-group target-group)
7137         (let ((unreads (gnus-group-group-unread)))
7138           (if (and (or (eq t unreads)
7139                        (and unreads (not (zerop unreads))))
7140                    (gnus-summary-read-group
7141                     target-group nil no-article
7142                     (and (buffer-name current-buffer) current-buffer)
7143                     nil backward))
7144               (setq entered t)
7145             (setq current-group target-group
7146                   target-group nil)))))))
7147
7148 (defun gnus-summary-prev-group (&optional no-article)
7149   "Exit current newsgroup and then select previous unread newsgroup.
7150 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7151   (interactive "P")
7152   (gnus-summary-next-group no-article nil t))
7153
7154 ;; Walking around summary lines.
7155
7156 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7157   "Go to the first subject satisfying any non-nil constraint.
7158 If UNREAD is non-nil, the article should be unread.
7159 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7160 If UNSEEN is non-nil, the article should be unseen.
7161 Returns the article selected or nil if there are no matching articles."
7162   (interactive "P")
7163   (cond
7164    ;; Empty summary.
7165    ((null gnus-newsgroup-data)
7166     (gnus-message 3 "No articles in the group")
7167     nil)
7168    ;; Pick the first article.
7169    ((not (or unread undownloaded unseen))
7170     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7171     (gnus-data-number (car gnus-newsgroup-data)))
7172    ;; Find the first unread article.
7173    (t
7174     (let ((data gnus-newsgroup-data))
7175       (while (and data
7176                   (let ((num (gnus-data-number (car data))))
7177                     (or (memq num gnus-newsgroup-unfetched)
7178                         (not (or (and unread
7179                                       (memq num gnus-newsgroup-unreads))
7180                                  (and undownloaded
7181                                       (memq num gnus-newsgroup-undownloaded))
7182                                  (and unseen
7183                                       (memq num gnus-newsgroup-unseen)))))))
7184         (setq data (cdr data)))
7185       (prog1
7186           (if data
7187               (progn
7188                 (goto-char (gnus-data-pos (car data)))
7189                 (gnus-data-number (car data)))
7190             (gnus-message 3 "No more%s articles"
7191                           (let* ((r (when unread " unread"))
7192                                  (d (when undownloaded " undownloaded"))
7193                                  (s (when unseen " unseen"))
7194                                  (l (delq nil (list r d s))))
7195                             (cond ((= 3 (length l))
7196                                    (concat r "," d ", or" s))
7197                                   ((= 2 (length l))
7198                                    (concat (car l) ", or" (cadr l)))
7199                                   ((= 1 (length l))
7200                                    (car l))
7201                                   (t
7202                                    ""))))
7203             nil
7204             )
7205         (gnus-summary-position-point))))))
7206
7207 (defun gnus-summary-next-subject (n &optional unread dont-display)
7208   "Go to next N'th summary line.
7209 If N is negative, go to the previous N'th subject line.
7210 If UNREAD is non-nil, only unread articles are selected.
7211 The difference between N and the actual number of steps taken is
7212 returned."
7213   (interactive "p")
7214   (let ((backward (< n 0))
7215         (n (abs n)))
7216     (while (and (> n 0)
7217                 (if backward
7218                     (gnus-summary-find-prev unread)
7219                   (gnus-summary-find-next unread)))
7220       (unless (zerop (setq n (1- n)))
7221         (gnus-summary-show-thread)))
7222     (when (/= 0 n)
7223       (gnus-message 7 "No more%s articles"
7224                     (if unread " unread" "")))
7225     (unless dont-display
7226       (gnus-summary-recenter)
7227       (gnus-summary-position-point))
7228     n))
7229
7230 (defun gnus-summary-next-unread-subject (n)
7231   "Go to next N'th unread summary line."
7232   (interactive "p")
7233   (gnus-summary-next-subject n t))
7234
7235 (defun gnus-summary-prev-subject (n &optional unread)
7236   "Go to previous N'th summary line.
7237 If optional argument UNREAD is non-nil, only unread article is selected."
7238   (interactive "p")
7239   (gnus-summary-next-subject (- n) unread))
7240
7241 (defun gnus-summary-prev-unread-subject (n)
7242   "Go to previous N'th unread summary line."
7243   (interactive "p")
7244   (gnus-summary-next-subject (- n) t))
7245
7246 (defun gnus-summary-goto-subjects (articles)
7247   "Insert the subject header for ARTICLES in the current buffer."
7248   (save-excursion
7249     (dolist (article articles)
7250       (gnus-summary-goto-subject article t)))
7251   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7252   (gnus-summary-position-point))
7253
7254 (defun gnus-summary-goto-subject (article &optional force silent)
7255   "Go the subject line of ARTICLE.
7256 If FORCE, also allow jumping to articles not currently shown."
7257   (interactive "nArticle number: ")
7258   (unless (numberp article)
7259     (error "Article %s is not a number" article))
7260   (let ((b (point))
7261         (data (gnus-data-find article)))
7262     ;; We read in the article if we have to.
7263     (and (not data)
7264          force
7265          (gnus-summary-insert-subject
7266           article
7267           (if (or (numberp force) (vectorp force)) force)
7268           t)
7269          (setq data (gnus-data-find article)))
7270     (goto-char b)
7271     (if (not data)
7272         (progn
7273           (unless silent
7274             (gnus-message 3 "Can't find article %d" article))
7275           nil)
7276       (let ((pt (gnus-data-pos data)))
7277         (goto-char pt)
7278         (gnus-summary-set-article-display-arrow pt))
7279       (gnus-summary-position-point)
7280       article)))
7281
7282 ;; Walking around summary lines with displaying articles.
7283
7284 (defun gnus-summary-expand-window (&optional arg)
7285   "Make the summary buffer take up the entire Emacs frame.
7286 Given a prefix, will force an `article' buffer configuration."
7287   (interactive "P")
7288   (if arg
7289       (gnus-configure-windows 'article 'force)
7290     (gnus-configure-windows 'summary 'force)))
7291
7292 (defun gnus-summary-display-article (article &optional all-header)
7293   "Display ARTICLE in article buffer."
7294   (when (gnus-buffer-live-p gnus-article-buffer)
7295     (with-current-buffer gnus-article-buffer
7296       (mm-enable-multibyte)))
7297   (gnus-set-global-variables)
7298   (when (gnus-buffer-live-p gnus-article-buffer)
7299     (with-current-buffer gnus-article-buffer
7300       (setq gnus-article-charset gnus-newsgroup-charset)
7301       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7302       (mm-enable-multibyte)))
7303   (if (null article)
7304       nil
7305     (prog1
7306         (if gnus-summary-display-article-function
7307             (funcall gnus-summary-display-article-function article all-header)
7308           (gnus-article-prepare article all-header))
7309       (gnus-run-hooks 'gnus-select-article-hook)
7310       (when (and gnus-current-article
7311                  (not (zerop gnus-current-article)))
7312         (gnus-summary-goto-subject gnus-current-article))
7313       (gnus-summary-recenter)
7314       (when (and gnus-use-trees gnus-show-threads)
7315         (gnus-possibly-generate-tree article)
7316         (gnus-highlight-selected-tree article))
7317       ;; Successfully display article.
7318       (gnus-article-set-window-start
7319        (cdr (assq article gnus-newsgroup-bookmarks))))))
7320
7321 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7322   "Select the current article.
7323 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7324 non-nil, the article will be re-fetched even if it already present in
7325 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7326 be displayed."
7327   ;; Make sure we are in the summary buffer to work around bbdb bug.
7328   (unless (eq major-mode 'gnus-summary-mode)
7329     (set-buffer gnus-summary-buffer))
7330   (let ((article (or article (gnus-summary-article-number)))
7331         (all-headers (not (not all-headers))) ;Must be t or nil.
7332         gnus-summary-display-article-function)
7333     (and (not pseudo)
7334          (gnus-summary-article-pseudo-p article)
7335          (error "This is a pseudo-article"))
7336     (save-excursion
7337       (set-buffer gnus-summary-buffer)
7338       (if (or (and gnus-single-article-buffer
7339                    (or (null gnus-current-article)
7340                        (null gnus-article-current)
7341                        (null (get-buffer gnus-article-buffer))
7342                        (not (eq article (cdr gnus-article-current)))
7343                        (not (equal (car gnus-article-current)
7344                                    gnus-newsgroup-name))))
7345               (and (not gnus-single-article-buffer)
7346                    (or (null gnus-current-article)
7347                        (not (eq gnus-current-article article))))
7348               force)
7349           ;; The requested article is different from the current article.
7350           (progn
7351             (gnus-summary-display-article article all-headers)
7352             (when (gnus-buffer-live-p gnus-article-buffer)
7353               (with-current-buffer gnus-article-buffer
7354                 (if (not gnus-article-decoded-p) ;; a local variable
7355                     (mm-disable-multibyte))))
7356             (gnus-article-set-window-start
7357              (cdr (assq article gnus-newsgroup-bookmarks)))
7358             article)
7359         'old))))
7360
7361 (defun gnus-summary-force-verify-and-decrypt ()
7362   "Display buttons for signed/encrypted parts and verify/decrypt them."
7363   (interactive)
7364   (let ((mm-verify-option 'known)
7365         (mm-decrypt-option 'known)
7366         (gnus-article-emulate-mime t)
7367         (gnus-buttonized-mime-types (append (list "multipart/signed"
7368                                                   "multipart/encrypted")
7369                                             gnus-buttonized-mime-types)))
7370     (gnus-summary-select-article nil 'force)))
7371
7372 (defun gnus-summary-set-current-mark (&optional current-mark)
7373   "Obsolete function."
7374   nil)
7375
7376 (defun gnus-summary-next-article (&optional unread subject backward push)
7377   "Select the next article.
7378 If UNREAD, only unread articles are selected.
7379 If SUBJECT, only articles with SUBJECT are selected.
7380 If BACKWARD, the previous article is selected instead of the next."
7381   (interactive "P")
7382   (cond
7383    ;; Is there such an article?
7384    ((and (gnus-summary-search-forward unread subject backward)
7385          (or (gnus-summary-display-article (gnus-summary-article-number))
7386              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7387     (gnus-summary-position-point))
7388    ;; If not, we try the first unread, if that is wanted.
7389    ((and subject
7390          gnus-auto-select-same
7391          (gnus-summary-first-unread-article))
7392     (gnus-summary-position-point)
7393     (gnus-message 6 "Wrapped"))
7394    ;; Try to get next/previous article not displayed in this group.
7395    ((and gnus-auto-extend-newsgroup
7396          (not unread) (not subject))
7397     (gnus-summary-goto-article
7398      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7399      nil (count-lines (point-min) (point))))
7400    ;; Go to next/previous group.
7401    (t
7402     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7403       (gnus-summary-jump-to-group gnus-newsgroup-name))
7404     (let ((cmd last-command-char)
7405           (point
7406            (with-current-buffer gnus-group-buffer
7407              (point)))
7408           (group
7409            (if (eq gnus-keep-same-level 'best)
7410                (gnus-summary-best-group gnus-newsgroup-name)
7411              (gnus-summary-search-group backward gnus-keep-same-level))))
7412       ;; For some reason, the group window gets selected.  We change
7413       ;; it back.
7414       (select-window (get-buffer-window (current-buffer)))
7415       ;; Select next unread newsgroup automagically.
7416       (cond
7417        ((or (not gnus-auto-select-next)
7418             (not cmd))
7419         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7420        ((or (eq gnus-auto-select-next 'quietly)
7421             (and (eq gnus-auto-select-next 'slightly-quietly)
7422                  push)
7423             (and (eq gnus-auto-select-next 'almost-quietly)
7424                  (gnus-summary-last-article-p)))
7425         ;; Select quietly.
7426         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7427             (gnus-summary-exit)
7428           (gnus-message 7 "No more%s articles (%s)..."
7429                         (if unread " unread" "")
7430                         (if group (concat "selecting " group)
7431                           "exiting"))
7432           (gnus-summary-next-group nil group backward)))
7433        (t
7434         (when (gnus-key-press-event-p last-input-event)
7435           (gnus-summary-walk-group-buffer
7436            gnus-newsgroup-name cmd unread backward point))))))))
7437
7438 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7439   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7440                       (?\C-p (gnus-group-prev-unread-group 1))))
7441         (cursor-in-echo-area t)
7442         keve key group ended prompt)
7443     (save-excursion
7444       (set-buffer gnus-group-buffer)
7445       (goto-char start)
7446       (setq group
7447             (if (eq gnus-keep-same-level 'best)
7448                 (gnus-summary-best-group gnus-newsgroup-name)
7449               (gnus-summary-search-group backward gnus-keep-same-level))))
7450     (while (not ended)
7451       (setq prompt
7452             (format
7453              "No more%s articles%s " (if unread " unread" "")
7454              (if (and group
7455                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7456                  (format " (Type %s for %s [%s])"
7457                          (single-key-description cmd)
7458                          (gnus-group-decoded-name group)
7459                          (gnus-group-unread group))
7460                (format " (Type %s to exit %s)"
7461                        (single-key-description cmd)
7462                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7463       ;; Confirm auto selection.
7464       (setq key (car (setq keve (gnus-read-event-char prompt)))
7465             ended t)
7466       (cond
7467        ((assq key keystrokes)
7468         (let ((obuf (current-buffer)))
7469           (switch-to-buffer gnus-group-buffer)
7470           (when group
7471             (gnus-group-jump-to-group group))
7472           (eval (cadr (assq key keystrokes)))
7473           (setq group (gnus-group-group-name))
7474           (switch-to-buffer obuf))
7475         (setq ended nil))
7476        ((equal key cmd)
7477         (if (or (not group)
7478                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7479             (gnus-summary-exit)
7480           (gnus-summary-next-group nil group backward)))
7481        (t
7482         (push (cdr keve) unread-command-events))))))
7483
7484 (defun gnus-summary-next-unread-article ()
7485   "Select unread article after current one."
7486   (interactive)
7487   (gnus-summary-next-article
7488    (or (not (eq gnus-summary-goto-unread 'never))
7489        (gnus-summary-last-article-p (gnus-summary-article-number)))
7490    (and gnus-auto-select-same
7491         (gnus-summary-article-subject))))
7492
7493 (defun gnus-summary-prev-article (&optional unread subject)
7494   "Select the article after the current one.
7495 If UNREAD is non-nil, only unread articles are selected."
7496   (interactive "P")
7497   (gnus-summary-next-article unread subject t))
7498
7499 (defun gnus-summary-prev-unread-article ()
7500   "Select unread article before current one."
7501   (interactive)
7502   (gnus-summary-prev-article
7503    (or (not (eq gnus-summary-goto-unread 'never))
7504        (gnus-summary-first-article-p (gnus-summary-article-number)))
7505    (and gnus-auto-select-same
7506         (gnus-summary-article-subject))))
7507
7508 (defun gnus-summary-next-page (&optional lines circular stop)
7509   "Show next page of the selected article.
7510 If at the end of the current article, select the next article.
7511 LINES says how many lines should be scrolled up.
7512
7513 If CIRCULAR is non-nil, go to the start of the article instead of
7514 selecting the next article when reaching the end of the current
7515 article.
7516
7517 If STOP is non-nil, just stop when reaching the end of the message.
7518
7519 Also see the variable `gnus-article-skip-boring'."
7520   (interactive "P")
7521   (setq gnus-summary-buffer (current-buffer))
7522   (gnus-set-global-variables)
7523   (let ((article (gnus-summary-article-number))
7524         (article-window (get-buffer-window gnus-article-buffer t))
7525         endp)
7526     ;; If the buffer is empty, we have no article.
7527     (unless article
7528       (error "No article to select"))
7529     (gnus-configure-windows 'article)
7530     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7531         (if (and (eq gnus-summary-goto-unread 'never)
7532                  (not (gnus-summary-last-article-p article)))
7533             (gnus-summary-next-article)
7534           (gnus-summary-next-unread-article))
7535       (if (or (null gnus-current-article)
7536               (null gnus-article-current)
7537               (/= article (cdr gnus-article-current))
7538               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7539           ;; Selected subject is different from current article's.
7540           (gnus-summary-display-article article)
7541         (when article-window
7542           (gnus-eval-in-buffer-window gnus-article-buffer
7543             (setq endp (or (gnus-article-next-page lines)
7544                            (gnus-article-only-boring-p))))
7545           (when endp
7546             (cond (stop
7547                    (gnus-message 3 "End of message"))
7548                   (circular
7549                    (gnus-summary-beginning-of-article))
7550                   (lines
7551                    (gnus-message 3 "End of message"))
7552                   ((null lines)
7553                    (if (and (eq gnus-summary-goto-unread 'never)
7554                             (not (gnus-summary-last-article-p article)))
7555                        (gnus-summary-next-article)
7556                      (gnus-summary-next-unread-article))))))))
7557     (gnus-summary-recenter)
7558     (gnus-summary-position-point)))
7559
7560 (defun gnus-summary-prev-page (&optional lines move)
7561   "Show previous page of selected article.
7562 Argument LINES specifies lines to be scrolled down.
7563 If MOVE, move to the previous unread article if point is at
7564 the beginning of the buffer."
7565   (interactive "P")
7566   (let ((article (gnus-summary-article-number))
7567         (article-window (get-buffer-window gnus-article-buffer t))
7568         endp)
7569     (gnus-configure-windows 'article)
7570     (if (or (null gnus-current-article)
7571             (null gnus-article-current)
7572             (/= article (cdr gnus-article-current))
7573             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7574         ;; Selected subject is different from current article's.
7575         (gnus-summary-display-article article)
7576       (gnus-summary-recenter)
7577       (when article-window
7578         (gnus-eval-in-buffer-window gnus-article-buffer
7579           (setq endp (gnus-article-prev-page lines)))
7580         (when (and move endp)
7581           (cond (lines
7582                  (gnus-message 3 "Beginning of message"))
7583                 ((null lines)
7584                  (if (and (eq gnus-summary-goto-unread 'never)
7585                           (not (gnus-summary-first-article-p article)))
7586                      (gnus-summary-prev-article)
7587                    (gnus-summary-prev-unread-article))))))))
7588   (gnus-summary-position-point))
7589
7590 (defun gnus-summary-prev-page-or-article (&optional lines)
7591   "Show previous page of selected article.
7592 Argument LINES specifies lines to be scrolled down.
7593 If at the beginning of the article, go to the next article."
7594   (interactive "P")
7595   (gnus-summary-prev-page lines t))
7596
7597 (defun gnus-summary-scroll-up (lines)
7598   "Scroll up (or down) one line current article.
7599 Argument LINES specifies lines to be scrolled up (or down if negative)."
7600   (interactive "p")
7601   (gnus-configure-windows 'article)
7602   (gnus-summary-show-thread)
7603   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7604     (gnus-eval-in-buffer-window gnus-article-buffer
7605       (cond ((> lines 0)
7606              (when (gnus-article-next-page lines)
7607                (gnus-message 3 "End of message")))
7608             ((< lines 0)
7609              (gnus-article-prev-page (- lines))))))
7610   (gnus-summary-recenter)
7611   (gnus-summary-position-point))
7612
7613 (defun gnus-summary-scroll-down (lines)
7614   "Scroll down (or up) one line current article.
7615 Argument LINES specifies lines to be scrolled down (or up if negative)."
7616   (interactive "p")
7617   (gnus-summary-scroll-up (- lines)))
7618
7619 (defun gnus-summary-next-same-subject ()
7620   "Select next article which has the same subject as current one."
7621   (interactive)
7622   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7623
7624 (defun gnus-summary-prev-same-subject ()
7625   "Select previous article which has the same subject as current one."
7626   (interactive)
7627   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7628
7629 (defun gnus-summary-next-unread-same-subject ()
7630   "Select next unread article which has the same subject as current one."
7631   (interactive)
7632   (gnus-summary-next-article t (gnus-summary-article-subject)))
7633
7634 (defun gnus-summary-prev-unread-same-subject ()
7635   "Select previous unread article which has the same subject as current one."
7636   (interactive)
7637   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7638
7639 (defun gnus-summary-first-unread-article ()
7640   "Select the first unread article.
7641 Return nil if there are no unread articles."
7642   (interactive)
7643   (prog1
7644       (when (gnus-summary-first-subject t)
7645         (gnus-summary-show-thread)
7646         (gnus-summary-first-subject t)
7647         (gnus-summary-display-article (gnus-summary-article-number)))
7648     (gnus-summary-position-point)))
7649
7650 (defun gnus-summary-first-unread-subject ()
7651   "Place the point on the subject line of the first unread article.
7652 Return nil if there are no unread articles."
7653   (interactive)
7654   (prog1
7655       (when (gnus-summary-first-subject t)
7656         (gnus-summary-show-thread)
7657         (gnus-summary-first-subject t))
7658     (gnus-summary-position-point)))
7659
7660 (defun gnus-summary-first-unseen-subject ()
7661   "Place the point on the subject line of the first unseen article.
7662 Return nil if there are no unseen articles."
7663   (interactive)
7664   (prog1
7665       (when (gnus-summary-first-subject nil nil t)
7666         (gnus-summary-show-thread)
7667         (gnus-summary-first-subject nil nil t))
7668     (gnus-summary-position-point)))
7669
7670 (defun gnus-summary-first-unseen-or-unread-subject ()
7671   "Place the point on the subject line of the first unseen article or,
7672 if all article have been seen, on the subject line of the first unread
7673 article."
7674   (interactive)
7675   (prog1
7676       (unless (when (gnus-summary-first-subject nil nil t)
7677                 (gnus-summary-show-thread)
7678                 (gnus-summary-first-subject nil nil t))
7679         (when (gnus-summary-first-subject t)
7680           (gnus-summary-show-thread)
7681           (gnus-summary-first-subject t)))
7682     (gnus-summary-position-point)))
7683
7684 (defun gnus-summary-first-article ()
7685   "Select the first article.
7686 Return nil if there are no articles."
7687   (interactive)
7688   (prog1
7689       (when (gnus-summary-first-subject)
7690         (gnus-summary-show-thread)
7691         (gnus-summary-first-subject)
7692         (gnus-summary-display-article (gnus-summary-article-number)))
7693     (gnus-summary-position-point)))
7694
7695 (defun gnus-summary-best-unread-article (&optional arg)
7696   "Select the unread article with the highest score.
7697 If given a prefix argument, select the next unread article that has a
7698 score higher than the default score."
7699   (interactive "P")
7700   (let ((article (if arg
7701                      (gnus-summary-better-unread-subject)
7702                    (gnus-summary-best-unread-subject))))
7703     (if article
7704         (gnus-summary-goto-article article)
7705       (error "No unread articles"))))
7706
7707 (defun gnus-summary-best-unread-subject ()
7708   "Select the unread subject with the highest score."
7709   (interactive)
7710   (let ((best -1000000)
7711         (data gnus-newsgroup-data)
7712         article score)
7713     (while data
7714       (and (gnus-data-unread-p (car data))
7715            (> (setq score
7716                     (gnus-summary-article-score (gnus-data-number (car data))))
7717               best)
7718            (setq best score
7719                  article (gnus-data-number (car data))))
7720       (setq data (cdr data)))
7721     (when article
7722       (gnus-summary-goto-subject article))
7723     (gnus-summary-position-point)
7724     article))
7725
7726 (defun gnus-summary-better-unread-subject ()
7727   "Select the first unread subject that has a score over the default score."
7728   (interactive)
7729   (let ((data gnus-newsgroup-data)
7730         article score)
7731     (while (and (setq article (gnus-data-number (car data)))
7732                 (or (gnus-data-read-p (car data))
7733                     (not (> (gnus-summary-article-score article)
7734                             gnus-summary-default-score))))
7735       (setq data (cdr data)))
7736     (when article
7737       (gnus-summary-goto-subject article))
7738     (gnus-summary-position-point)
7739     article))
7740
7741 (defun gnus-summary-last-subject ()
7742   "Go to the last displayed subject line in the group."
7743   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7744     (when article
7745       (gnus-summary-goto-subject article))))
7746
7747 (defun gnus-summary-goto-article (article &optional all-headers force)
7748   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7749 If ALL-HEADERS is non-nil, no header lines are hidden.
7750 If FORCE, go to the article even if it isn't displayed.  If FORCE
7751 is a number, it is the line the article is to be displayed on."
7752   (interactive
7753    (list
7754     (completing-read
7755      "Article number or Message-ID: "
7756      (mapcar (lambda (number) (list (int-to-string number)))
7757              gnus-newsgroup-limit))
7758     current-prefix-arg
7759     t))
7760   (prog1
7761       (if (and (stringp article)
7762                (string-match "@\\|%40" article))
7763           (gnus-summary-refer-article article)
7764         (when (stringp article)
7765           (setq article (string-to-number article)))
7766         (if (gnus-summary-goto-subject article force)
7767             (gnus-summary-display-article article all-headers)
7768           (gnus-message 4 "Couldn't go to article %s" article) nil))
7769     (gnus-summary-position-point)))
7770
7771 (defun gnus-summary-goto-last-article ()
7772   "Go to the previously read article."
7773   (interactive)
7774   (prog1
7775       (when gnus-last-article
7776         (gnus-summary-goto-article gnus-last-article nil t))
7777     (gnus-summary-position-point)))
7778
7779 (defun gnus-summary-pop-article (number)
7780   "Pop one article off the history and go to the previous.
7781 NUMBER articles will be popped off."
7782   (interactive "p")
7783   (let (to)
7784     (setq gnus-newsgroup-history
7785           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7786     (if to
7787         (gnus-summary-goto-article (car to) nil t)
7788       (error "Article history empty")))
7789   (gnus-summary-position-point))
7790
7791 ;; Summary commands and functions for limiting the summary buffer.
7792
7793 (defun gnus-summary-limit-to-articles (n)
7794   "Limit the summary buffer to the next N articles.
7795 If not given a prefix, use the process marked articles instead."
7796   (interactive "P")
7797   (prog1
7798       (let ((articles (gnus-summary-work-articles n)))
7799         (setq gnus-newsgroup-processable nil)
7800         (gnus-summary-limit articles))
7801     (gnus-summary-position-point)))
7802
7803 (defun gnus-summary-pop-limit (&optional total)
7804   "Restore the previous limit.
7805 If given a prefix, remove all limits."
7806   (interactive "P")
7807   (when total
7808     (setq gnus-newsgroup-limits
7809           (list (mapcar (lambda (h) (mail-header-number h))
7810                         gnus-newsgroup-headers))))
7811   (unless gnus-newsgroup-limits
7812     (error "No limit to pop"))
7813   (prog1
7814       (gnus-summary-limit nil 'pop)
7815     (gnus-summary-position-point)))
7816
7817 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7818   "Limit the summary buffer to articles that have subjects that match a regexp.
7819 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7820   (interactive
7821    (list (read-string (if current-prefix-arg
7822                           "Exclude subject (regexp): "
7823                         "Limit to subject (regexp): "))
7824          nil current-prefix-arg))
7825   (unless header
7826     (setq header "subject"))
7827   (when (not (equal "" subject))
7828     (prog1
7829         (let ((articles (gnus-summary-find-matching
7830                          (or header "subject") subject 'all nil nil
7831                          not-matching)))
7832           (unless articles
7833             (error "Found no matches for \"%s\"" subject))
7834           (gnus-summary-limit articles))
7835       (gnus-summary-position-point))))
7836
7837 (defun gnus-summary-limit-to-author (from &optional not-matching)
7838   "Limit the summary buffer to articles that have authors that match a regexp.
7839 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7840   (interactive
7841    (list (read-string (if current-prefix-arg
7842                           "Exclude author (regexp): "
7843                         "Limit to author (regexp): "))
7844          current-prefix-arg))
7845   (gnus-summary-limit-to-subject from "from" not-matching))
7846
7847 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
7848   "Limit the summary buffer to articles with the given RECIPIENT.
7849
7850 If NOT-MATCHING, exclude RECIPIENT.
7851
7852 To and Cc headers are checked.  You need to include them in
7853 `nnmail-extra-headers'."
7854   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
7855   (interactive
7856    (list (read-string (format "%s recipient (regexp): "
7857                               (if current-prefix-arg "Exclude" "Limit to")))
7858          current-prefix-arg))
7859   (when (not (equal "" recipient))
7860     (prog1 (let* ((to
7861                    (if (memq 'To nnmail-extra-headers)
7862                        (gnus-summary-find-matching
7863                         (cons 'extra 'To) recipient 'all nil nil
7864                         not-matching)
7865                      (gnus-message
7866                       1 "`To' isn't present in `nnmail-extra-headers'")
7867                      (sit-for 1)
7868                      nil))
7869                   (cc
7870                    (if (memq 'Cc nnmail-extra-headers)
7871                        (gnus-summary-find-matching
7872                         (cons 'extra 'Cc) recipient 'all nil nil
7873                         not-matching)
7874                      (gnus-message
7875                       1 "`Cc' isn't present in `nnmail-extra-headers'")
7876                      (sit-for 1)
7877                      nil))
7878                   (articles
7879                    (if not-matching
7880                        ;; We need the numbers that are in both lists:
7881                        (mapcar (lambda (a)
7882                                  (and (memq a to) a))
7883                                cc)
7884                      (nconc to cc))))
7885              (unless articles
7886                (error "Found no matches for \"%s\"" recipient))
7887              (gnus-summary-limit articles))
7888       (gnus-summary-position-point))))
7889
7890 (defun gnus-summary-limit-to-age (age &optional younger-p)
7891   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7892 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7893 articles that are younger than AGE days."
7894   (interactive
7895    (let ((younger current-prefix-arg)
7896          (days-got nil)
7897          days)
7898      (while (not days-got)
7899        (setq days (if younger
7900                       (read-string "Limit to articles younger than (in days, older when negative): ")
7901                     (read-string
7902                      "Limit to articles older than (in days, younger when negative): ")))
7903        (when (> (length days) 0)
7904          (setq days (read days)))
7905        (if (numberp days)
7906            (progn
7907              (setq days-got t)
7908              (when (< days 0)
7909                (setq younger (not younger))
7910                (setq days (* days -1))))
7911          (message "Please enter a number.")
7912          (sleep-for 1)))
7913      (list days younger)))
7914   (prog1
7915       (let ((data gnus-newsgroup-data)
7916             (cutoff (days-to-time age))
7917             articles d date is-younger)
7918         (while (setq d (pop data))
7919           (when (and (vectorp (gnus-data-header d))
7920                      (setq date (mail-header-date (gnus-data-header d))))
7921             (setq is-younger (time-less-p
7922                               (time-since (condition-case ()
7923                                               (date-to-time date)
7924                                             (error '(0 0))))
7925                               cutoff))
7926             (when (if younger-p
7927                       is-younger
7928                     (not is-younger))
7929               (push (gnus-data-number d) articles))))
7930         (gnus-summary-limit (nreverse articles)))
7931     (gnus-summary-position-point)))
7932
7933 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7934   "Limit the summary buffer to articles that match an 'extra' header."
7935   (interactive
7936    (let ((header
7937           (intern
7938            (gnus-completing-read-with-default
7939             (symbol-name (car gnus-extra-headers))
7940             (if current-prefix-arg
7941                 "Exclude extra header"
7942               "Limit extra header")
7943             (mapcar (lambda (x)
7944                       (cons (symbol-name x) x))
7945                     gnus-extra-headers)
7946             nil
7947             t))))
7948      (list header
7949            (read-string (format "%s header %s (regexp): "
7950                                 (if current-prefix-arg "Exclude" "Limit to")
7951                                 header))
7952            current-prefix-arg)))
7953   (when (not (equal "" regexp))
7954     (prog1
7955         (let ((articles (gnus-summary-find-matching
7956                          (cons 'extra header) regexp 'all nil nil
7957                          not-matching)))
7958           (unless articles
7959             (error "Found no matches for \"%s\"" regexp))
7960           (gnus-summary-limit articles))
7961       (gnus-summary-position-point))))
7962
7963 (defun gnus-summary-limit-to-display-predicate ()
7964   "Limit the summary buffer to the predicated in the `display' group parameter."
7965   (interactive)
7966   (unless gnus-newsgroup-display
7967     (error "There is no `display' group parameter"))
7968   (let (articles)
7969     (dolist (number gnus-newsgroup-articles)
7970       (when (funcall gnus-newsgroup-display)
7971         (push number articles)))
7972     (gnus-summary-limit articles))
7973   (gnus-summary-position-point))
7974
7975 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7976 (make-obsolete
7977  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7978
7979 (defun gnus-summary-limit-to-unread (&optional all)
7980   "Limit the summary buffer to articles that are not marked as read.
7981 If ALL is non-nil, limit strictly to unread articles."
7982   (interactive "P")
7983   (if all
7984       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7985     (gnus-summary-limit-to-marks
7986      ;; Concat all the marks that say that an article is read and have
7987      ;; those removed.
7988      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7989            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7990            gnus-low-score-mark gnus-expirable-mark
7991            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7992            gnus-duplicate-mark gnus-souped-mark)
7993      'reverse)))
7994
7995 (defun gnus-summary-limit-to-replied (&optional unreplied)
7996   "Limit the summary buffer to replied articles.
7997 If UNREPLIED (the prefix), limit to unreplied articles."
7998   (interactive "P")
7999   (if unreplied
8000       (gnus-summary-limit
8001        (gnus-set-difference gnus-newsgroup-articles
8002         gnus-newsgroup-replied))
8003     (gnus-summary-limit gnus-newsgroup-replied))
8004   (gnus-summary-position-point))
8005
8006 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8007 (make-obsolete 'gnus-summary-delete-marked-with
8008                'gnus-summary-limit-exclude-marks)
8009
8010 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8011   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8012 If REVERSE, limit the summary buffer to articles that are marked
8013 with MARKS.  MARKS can either be a string of marks or a list of marks.
8014 Returns how many articles were removed."
8015   (interactive "sMarks: ")
8016   (gnus-summary-limit-to-marks marks t))
8017
8018 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8019   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8020 If REVERSE (the prefix), limit the summary buffer to articles that are
8021 not marked with MARKS.  MARKS can either be a string of marks or a
8022 list of marks.
8023 Returns how many articles were removed."
8024   (interactive "sMarks: \nP")
8025   (prog1
8026       (let ((data gnus-newsgroup-data)
8027             (marks (if (listp marks) marks
8028                      (append marks nil))) ; Transform to list.
8029             articles)
8030         (while data
8031           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8032                   (memq (gnus-data-mark (car data)) marks))
8033             (push (gnus-data-number (car data)) articles))
8034           (setq data (cdr data)))
8035         (gnus-summary-limit articles))
8036     (gnus-summary-position-point)))
8037
8038 (defun gnus-summary-limit-to-score (score)
8039   "Limit to articles with score at or above SCORE."
8040   (interactive "NLimit to articles with score of at least: ")
8041   (let ((data gnus-newsgroup-data)
8042         articles)
8043     (while data
8044       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8045                 score)
8046         (push (gnus-data-number (car data)) articles))
8047       (setq data (cdr data)))
8048     (prog1
8049         (gnus-summary-limit articles)
8050       (gnus-summary-position-point))))
8051
8052 (defun gnus-summary-limit-to-unseen ()
8053   "Limit to unseen articles."
8054   (interactive)
8055   (prog1
8056       (gnus-summary-limit gnus-newsgroup-unseen)
8057     (gnus-summary-position-point)))
8058
8059 (defun gnus-summary-limit-include-thread (id)
8060   "Display all the hidden articles that is in the thread with ID in it.
8061 When called interactively, ID is the Message-ID of the current
8062 article."
8063   (interactive (list (mail-header-id (gnus-summary-article-header))))
8064   (let ((articles (gnus-articles-in-thread
8065                    (gnus-id-to-thread (gnus-root-id id)))))
8066     (prog1
8067         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8068       (gnus-summary-limit-include-matching-articles
8069        "subject"
8070        (regexp-quote (gnus-simplify-subject-re
8071                       (mail-header-subject (gnus-id-to-header id)))))
8072       (gnus-summary-position-point))))
8073
8074 (defun gnus-summary-limit-include-matching-articles (header regexp)
8075   "Display all the hidden articles that have HEADERs that match REGEXP."
8076   (interactive (list (read-string "Match on header: ")
8077                      (read-string "Regexp: ")))
8078   (let ((articles (gnus-find-matching-articles header regexp)))
8079     (prog1
8080         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8081       (gnus-summary-position-point))))
8082
8083 (defun gnus-summary-insert-dormant-articles ()
8084   "Insert all the dormant articles for this group into the current buffer."
8085   (interactive)
8086   (let ((gnus-verbose (max 6 gnus-verbose)))
8087     (if (not gnus-newsgroup-dormant)
8088         (gnus-message 3 "No cached articles for this group")
8089       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8090
8091 (defun gnus-summary-limit-include-dormant ()
8092   "Display all the hidden articles that are marked as dormant.
8093 Note that this command only works on a subset of the articles currently
8094 fetched for this group."
8095   (interactive)
8096   (unless gnus-newsgroup-dormant
8097     (error "There are no dormant articles in this group"))
8098   (prog1
8099       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8100     (gnus-summary-position-point)))
8101
8102 (defun gnus-summary-limit-exclude-dormant ()
8103   "Hide all dormant articles."
8104   (interactive)
8105   (prog1
8106       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8107     (gnus-summary-position-point)))
8108
8109 (defun gnus-summary-limit-exclude-childless-dormant ()
8110   "Hide all dormant articles that have no children."
8111   (interactive)
8112   (let ((data (gnus-data-list t))
8113         articles d children)
8114     ;; Find all articles that are either not dormant or have
8115     ;; children.
8116     (while (setq d (pop data))
8117       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8118                 (and (setq children
8119                            (gnus-article-children (gnus-data-number d)))
8120                      (let (found)
8121                        (while children
8122                          (when (memq (car children) articles)
8123                            (setq children nil
8124                                  found t))
8125                          (pop children))
8126                        found)))
8127         (push (gnus-data-number d) articles)))
8128     ;; Do the limiting.
8129     (prog1
8130         (gnus-summary-limit articles)
8131       (gnus-summary-position-point))))
8132
8133 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8134   "Mark all unread excluded articles as read.
8135 If ALL, mark even excluded ticked and dormants as read."
8136   (interactive "P")
8137   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8138   (let ((articles (gnus-sorted-ndifference
8139                    (sort
8140                     (mapcar (lambda (h) (mail-header-number h))
8141                             gnus-newsgroup-headers)
8142                     '<)
8143                    gnus-newsgroup-limit))
8144         article)
8145     (setq gnus-newsgroup-unreads
8146           (gnus-sorted-intersection gnus-newsgroup-unreads
8147                                     gnus-newsgroup-limit))
8148     (if all
8149         (setq gnus-newsgroup-dormant nil
8150               gnus-newsgroup-marked nil
8151               gnus-newsgroup-reads
8152               (nconc
8153                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8154                gnus-newsgroup-reads))
8155       (while (setq article (pop articles))
8156         (unless (or (memq article gnus-newsgroup-dormant)
8157                     (memq article gnus-newsgroup-marked))
8158           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8159
8160 (defun gnus-summary-limit (articles &optional pop)
8161   (if pop
8162       ;; We pop the previous limit off the stack and use that.
8163       (setq articles (car gnus-newsgroup-limits)
8164             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8165     ;; We use the new limit, so we push the old limit on the stack.
8166     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8167   ;; Set the limit.
8168   (setq gnus-newsgroup-limit articles)
8169   (let ((total (length gnus-newsgroup-data))
8170         (data (gnus-data-find-list (gnus-summary-article-number)))
8171         (gnus-summary-mark-below nil)   ; Inhibit this.
8172         found)
8173     ;; This will do all the work of generating the new summary buffer
8174     ;; according to the new limit.
8175     (gnus-summary-prepare)
8176     ;; Hide any threads, possibly.
8177     (gnus-summary-maybe-hide-threads)
8178     ;; Try to return to the article you were at, or one in the
8179     ;; neighborhood.
8180     (when data
8181       ;; We try to find some article after the current one.
8182       (while data
8183         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8184           (setq data nil
8185                 found t))
8186         (setq data (cdr data))))
8187     (unless found
8188       ;; If there is no data, that means that we were after the last
8189       ;; article.  The same goes when we can't find any articles
8190       ;; after the current one.
8191       (goto-char (point-max))
8192       (gnus-summary-find-prev))
8193     (gnus-set-mode-line 'summary)
8194     ;; We return how many articles were removed from the summary
8195     ;; buffer as a result of the new limit.
8196     (- total (length gnus-newsgroup-data))))
8197
8198 (defsubst gnus-invisible-cut-children (threads)
8199   (let ((num 0))
8200     (while threads
8201       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8202         (incf num))
8203       (pop threads))
8204     (< num 2)))
8205
8206 (defsubst gnus-cut-thread (thread)
8207   "Go forwards in the thread until we find an article that we want to display."
8208   (when (or (eq gnus-fetch-old-headers 'some)
8209             (eq gnus-fetch-old-headers 'invisible)
8210             (numberp gnus-fetch-old-headers)
8211             (eq gnus-build-sparse-threads 'some)
8212             (eq gnus-build-sparse-threads 'more))
8213     ;; Deal with old-fetched headers and sparse threads.
8214     (while (and
8215             thread
8216             (or
8217              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8218              (gnus-summary-article-ancient-p
8219               (mail-header-number (car thread))))
8220             (if (or (<= (length (cdr thread)) 1)
8221                     (eq gnus-fetch-old-headers 'invisible))
8222                 (setq gnus-newsgroup-limit
8223                       (delq (mail-header-number (car thread))
8224                             gnus-newsgroup-limit)
8225                       thread (cadr thread))
8226               (when (gnus-invisible-cut-children (cdr thread))
8227                 (let ((th (cdr thread)))
8228                   (while th
8229                     (if (memq (mail-header-number (caar th))
8230                               gnus-newsgroup-limit)
8231                         (setq thread (car th)
8232                               th nil)
8233                       (setq th (cdr th))))))))))
8234   thread)
8235
8236 (defun gnus-cut-threads (threads)
8237   "Cut off all uninteresting articles from the beginning of THREADS."
8238   (when (or (eq gnus-fetch-old-headers 'some)
8239             (eq gnus-fetch-old-headers 'invisible)
8240             (numberp gnus-fetch-old-headers)
8241             (eq gnus-build-sparse-threads 'some)
8242             (eq gnus-build-sparse-threads 'more))
8243     (let ((th threads))
8244       (while th
8245         (setcar th (gnus-cut-thread (car th)))
8246         (setq th (cdr th)))))
8247   ;; Remove nixed out threads.
8248   (delq nil threads))
8249
8250 (defun gnus-summary-initial-limit (&optional show-if-empty)
8251   "Figure out what the initial limit is supposed to be on group entry.
8252 This entails weeding out unwanted dormants, low-scored articles,
8253 fetch-old-headers verbiage, and so on."
8254   ;; Most groups have nothing to remove.
8255   (if (or gnus-inhibit-limiting
8256           (and (null gnus-newsgroup-dormant)
8257                (eq gnus-newsgroup-display 'gnus-not-ignore)
8258                (not (eq gnus-fetch-old-headers 'some))
8259                (not (numberp gnus-fetch-old-headers))
8260                (not (eq gnus-fetch-old-headers 'invisible))
8261                (null gnus-summary-expunge-below)
8262                (not (eq gnus-build-sparse-threads 'some))
8263                (not (eq gnus-build-sparse-threads 'more))
8264                (null gnus-thread-expunge-below)
8265                (not gnus-use-nocem)))
8266       ()                                ; Do nothing.
8267     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8268     (setq gnus-newsgroup-limit nil)
8269     (mapatoms
8270      (lambda (node)
8271        (unless (car (symbol-value node))
8272          ;; These threads have no parents -- they are roots.
8273          (let ((nodes (cdr (symbol-value node)))
8274                thread)
8275            (while nodes
8276              (if (and gnus-thread-expunge-below
8277                       (< (gnus-thread-total-score (car nodes))
8278                          gnus-thread-expunge-below))
8279                  (gnus-expunge-thread (pop nodes))
8280                (setq thread (pop nodes))
8281                (gnus-summary-limit-children thread))))))
8282      gnus-newsgroup-dependencies)
8283     ;; If this limitation resulted in an empty group, we might
8284     ;; pop the previous limit and use it instead.
8285     (when (and (not gnus-newsgroup-limit)
8286                show-if-empty)
8287       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8288     gnus-newsgroup-limit))
8289
8290 (defun gnus-summary-limit-children (thread)
8291   "Return 1 if this subthread is visible and 0 if it is not."
8292   ;; First we get the number of visible children to this thread.  This
8293   ;; is done by recursing down the thread using this function, so this
8294   ;; will really go down to a leaf article first, before slowly
8295   ;; working its way up towards the root.
8296   (when thread
8297     (let* ((max-lisp-eval-depth 5000)
8298            (children
8299            (if (cdr thread)
8300                (apply '+ (mapcar 'gnus-summary-limit-children
8301                                  (cdr thread)))
8302              0))
8303           (number (mail-header-number (car thread)))
8304           score)
8305       (if (and
8306            (not (memq number gnus-newsgroup-marked))
8307            (or
8308             ;; If this article is dormant and has absolutely no visible
8309             ;; children, then this article isn't visible.
8310             (and (memq number gnus-newsgroup-dormant)
8311                  (zerop children))
8312             ;; If this is "fetch-old-headered" and there is no
8313             ;; visible children, then we don't want this article.
8314             (and (or (eq gnus-fetch-old-headers 'some)
8315                      (numberp gnus-fetch-old-headers))
8316                  (gnus-summary-article-ancient-p number)
8317                  (zerop children))
8318             ;; If this is "fetch-old-headered" and `invisible', then
8319             ;; we don't want this article.
8320             (and (eq gnus-fetch-old-headers 'invisible)
8321                  (gnus-summary-article-ancient-p number))
8322             ;; If this is a sparsely inserted article with no children,
8323             ;; we don't want it.
8324             (and (eq gnus-build-sparse-threads 'some)
8325                  (gnus-summary-article-sparse-p number)
8326                  (zerop children))
8327             ;; If we use expunging, and this article is really
8328             ;; low-scored, then we don't want this article.
8329             (when (and gnus-summary-expunge-below
8330                        (< (setq score
8331                                 (or (cdr (assq number gnus-newsgroup-scored))
8332                                     gnus-summary-default-score))
8333                           gnus-summary-expunge-below))
8334               ;; We increase the expunge-tally here, but that has
8335               ;; nothing to do with the limits, really.
8336               (incf gnus-newsgroup-expunged-tally)
8337               ;; We also mark as read here, if that's wanted.
8338               (when (and gnus-summary-mark-below
8339                          (< score gnus-summary-mark-below))
8340                 (setq gnus-newsgroup-unreads
8341                       (delq number gnus-newsgroup-unreads))
8342                 (if gnus-newsgroup-auto-expire
8343                     (push number gnus-newsgroup-expirable)
8344                   (push (cons number gnus-low-score-mark)
8345                         gnus-newsgroup-reads)))
8346               t)
8347             ;; Do the `display' group parameter.
8348             (and gnus-newsgroup-display
8349                  (not (funcall gnus-newsgroup-display)))
8350             ;; Check NoCeM things.
8351             (when (and gnus-use-nocem
8352                        (gnus-nocem-unwanted-article-p
8353                         (mail-header-id (car thread))))
8354               (setq gnus-newsgroup-unreads
8355                     (delq number gnus-newsgroup-unreads))
8356               t)))
8357           ;; Nope, invisible article.
8358           0
8359         ;; Ok, this article is to be visible, so we add it to the limit
8360         ;; and return 1.
8361         (push number gnus-newsgroup-limit)
8362         1))))
8363
8364 (defun gnus-expunge-thread (thread)
8365   "Mark all articles in THREAD as read."
8366   (let* ((number (mail-header-number (car thread))))
8367     (incf gnus-newsgroup-expunged-tally)
8368     ;; We also mark as read here, if that's wanted.
8369     (setq gnus-newsgroup-unreads
8370           (delq number gnus-newsgroup-unreads))
8371     (if gnus-newsgroup-auto-expire
8372         (push number gnus-newsgroup-expirable)
8373       (push (cons number gnus-low-score-mark)
8374             gnus-newsgroup-reads)))
8375   ;; Go recursively through all subthreads.
8376   (mapcar 'gnus-expunge-thread (cdr thread)))
8377
8378 ;; Summary article oriented commands
8379
8380 (defun gnus-summary-refer-parent-article (n)
8381   "Refer parent article N times.
8382 If N is negative, go to ancestor -N instead.
8383 The difference between N and the number of articles fetched is returned."
8384   (interactive "p")
8385   (let ((skip 1)
8386         error header ref)
8387     (when (not (natnump n))
8388       (setq skip (abs n)
8389             n 1))
8390     (while (and (> n 0)
8391                 (not error))
8392       (setq header (gnus-summary-article-header))
8393       (if (and (eq (mail-header-number header)
8394                    (cdr gnus-article-current))
8395                (equal gnus-newsgroup-name
8396                       (car gnus-article-current)))
8397           ;; If we try to find the parent of the currently
8398           ;; displayed article, then we take a look at the actual
8399           ;; References header, since this is slightly more
8400           ;; reliable than the References field we got from the
8401           ;; server.
8402           (save-excursion
8403             (set-buffer gnus-original-article-buffer)
8404             (nnheader-narrow-to-headers)
8405             (unless (setq ref (message-fetch-field "references"))
8406               (when (setq ref (message-fetch-field "in-reply-to"))
8407                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8408             (widen))
8409         (setq ref
8410               ;; It's not the current article, so we take a bet on
8411               ;; the value we got from the server.
8412               (mail-header-references header)))
8413       (if (and ref
8414                (not (equal ref "")))
8415           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8416             (gnus-message 1 "Couldn't find parent"))
8417         (gnus-message 1 "No references in article %d"
8418                       (gnus-summary-article-number))
8419         (setq error t))
8420       (decf n))
8421     (gnus-summary-position-point)
8422     n))
8423
8424 (defun gnus-summary-refer-references ()
8425   "Fetch all articles mentioned in the References header.
8426 Return the number of articles fetched."
8427   (interactive)
8428   (let ((ref (mail-header-references (gnus-summary-article-header)))
8429         (current (gnus-summary-article-number))
8430         (n 0))
8431     (if (or (not ref)
8432             (equal ref ""))
8433         (error "No References in the current article")
8434       ;; For each Message-ID in the References header...
8435       (while (string-match "<[^>]*>" ref)
8436         (incf n)
8437         ;; ... fetch that article.
8438         (gnus-summary-refer-article
8439          (prog1 (match-string 0 ref)
8440            (setq ref (substring ref (match-end 0))))))
8441       (gnus-summary-goto-subject current)
8442       (gnus-summary-position-point)
8443       n)))
8444
8445 (defun gnus-summary-refer-thread (&optional limit)
8446   "Fetch all articles in the current thread.
8447 If LIMIT (the numerical prefix), fetch that many old headers instead
8448 of what's specified by the `gnus-refer-thread-limit' variable."
8449   (interactive "P")
8450   (let ((id (mail-header-id (gnus-summary-article-header)))
8451         (limit (if limit (prefix-numeric-value limit)
8452                  gnus-refer-thread-limit)))
8453     (unless (eq gnus-fetch-old-headers 'invisible)
8454       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8455       ;; Retrieve the headers and read them in.
8456       (if (eq (if (numberp limit)
8457                   (gnus-retrieve-headers
8458                    (list (min
8459                           (+ (mail-header-number
8460                               (gnus-summary-article-header))
8461                              limit)
8462                           gnus-newsgroup-end))
8463                    gnus-newsgroup-name (* limit 2))
8464                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8465                 ;; headers.
8466                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8467                                        gnus-newsgroup-name limit))
8468               'nov)
8469           (gnus-build-all-threads)
8470         (error "Can't fetch thread from back ends that don't support NOV"))
8471       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8472     (gnus-summary-limit-include-thread id)))
8473
8474 (defun gnus-summary-refer-article (message-id)
8475   "Fetch an article specified by MESSAGE-ID."
8476   (interactive "sMessage-ID: ")
8477   (when (and (stringp message-id)
8478              (not (zerop (length message-id))))
8479     (setq message-id (gnus-replace-in-string message-id " " ""))
8480     ;; Construct the correct Message-ID if necessary.
8481     ;; Suggested by tale@pawl.rpi.edu.
8482     (unless (string-match "^<" message-id)
8483       (setq message-id (concat "<" message-id)))
8484     (unless (string-match ">$" message-id)
8485       (setq message-id (concat message-id ">")))
8486     ;; People often post MIDs from URLs, so unhex it:
8487     (unless (string-match "@" message-id)
8488       (setq message-id (gnus-url-unhex-string message-id)))
8489     (let* ((header (gnus-id-to-header message-id))
8490            (sparse (and header
8491                         (gnus-summary-article-sparse-p
8492                          (mail-header-number header))
8493                         (memq (mail-header-number header)
8494                               gnus-newsgroup-limit)))
8495            number)
8496       (cond
8497        ;; If the article is present in the buffer we just go to it.
8498        ((and header
8499              (or (not (gnus-summary-article-sparse-p
8500                        (mail-header-number header)))
8501                  sparse))
8502         (prog1
8503             (gnus-summary-goto-article
8504              (mail-header-number header) nil t)
8505           (when sparse
8506             (gnus-summary-update-article (mail-header-number header)))))
8507        (t
8508         ;; We fetch the article.
8509         (catch 'found
8510           (dolist (gnus-override-method (gnus-refer-article-methods))
8511             (when (and (gnus-check-server gnus-override-method)
8512                        ;; Fetch the header,
8513                        (setq number (gnus-summary-insert-subject message-id)))
8514               ;; and display the article.
8515               (gnus-summary-select-article nil nil nil number)
8516               (throw 'found t)))
8517           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8518
8519 (defun gnus-refer-article-methods ()
8520   "Return a list of referable methods."
8521   (cond
8522    ;; No method, so we default to current and native.
8523    ((null gnus-refer-article-method)
8524     (list gnus-current-select-method gnus-select-method))
8525    ;; Current.
8526    ((eq 'current gnus-refer-article-method)
8527     (list gnus-current-select-method))
8528    ;; List of select methods.
8529    ((not (and (symbolp (car gnus-refer-article-method))
8530               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8531     (let (out)
8532       (dolist (method gnus-refer-article-method)
8533         (push (if (eq 'current method)
8534                   gnus-current-select-method
8535                 method)
8536               out))
8537       (nreverse out)))
8538    ;; One single select method.
8539    (t
8540     (list gnus-refer-article-method))))
8541
8542 (defun gnus-summary-edit-parameters ()
8543   "Edit the group parameters of the current group."
8544   (interactive)
8545   (gnus-group-edit-group gnus-newsgroup-name 'params))
8546
8547 (defun gnus-summary-customize-parameters ()
8548   "Customize the group parameters of the current group."
8549   (interactive)
8550   (gnus-group-customize gnus-newsgroup-name))
8551
8552 (defun gnus-summary-enter-digest-group (&optional force)
8553   "Enter an nndoc group based on the current article.
8554 If FORCE, force a digest interpretation.  If not, try
8555 to guess what the document format is."
8556   (interactive "P")
8557   (let ((conf gnus-current-window-configuration))
8558     (save-window-excursion
8559       (save-excursion
8560         (let (gnus-article-prepare-hook
8561               gnus-display-mime-function
8562               gnus-break-pages)
8563           (gnus-summary-select-article))))
8564     (setq gnus-current-window-configuration conf)
8565     (let* ((name (format "%s-%d"
8566                          (gnus-group-prefixed-name
8567                           gnus-newsgroup-name (list 'nndoc ""))
8568                          (with-current-buffer gnus-summary-buffer
8569                            gnus-current-article)))
8570            (ogroup gnus-newsgroup-name)
8571            (params (append (gnus-info-params (gnus-get-info ogroup))
8572                            (list (cons 'to-group ogroup))
8573                            (list (cons 'parent-group ogroup))
8574                            (list (cons 'save-article-group ogroup))))
8575            (case-fold-search t)
8576            (buf (current-buffer))
8577            dig to-address)
8578       (save-excursion
8579         (set-buffer gnus-original-article-buffer)
8580         ;; Have the digest group inherit the main mail address of
8581         ;; the parent article.
8582         (when (setq to-address (or (gnus-fetch-field "reply-to")
8583                                    (gnus-fetch-field "from")))
8584           (setq params (append
8585                         (list (cons 'to-address
8586                                     (funcall gnus-decode-encoded-word-function
8587                                              to-address))))))
8588         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8589         (insert-buffer-substring gnus-original-article-buffer)
8590         ;; Remove lines that may lead nndoc to misinterpret the
8591         ;; document type.
8592         (narrow-to-region
8593          (goto-char (point-min))
8594          (or (search-forward "\n\n" nil t) (point)))
8595         (goto-char (point-min))
8596         (delete-matching-lines "^Path:\\|^From ")
8597         (widen))
8598       (unwind-protect
8599           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8600                     (gnus-newsgroup-ephemeral-ignored-charsets
8601                      gnus-newsgroup-ignored-charsets))
8602                 (gnus-group-read-ephemeral-group
8603                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8604                               (nndoc-article-type
8605                                ,(if force 'mbox 'guess)))
8606                  t nil nil nil
8607                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8608                                                         "ADAPT")))))
8609               ;; Make all postings to this group go to the parent group.
8610               (nconc (gnus-info-params (gnus-get-info name))
8611                      params)
8612             ;; Couldn't select this doc group.
8613             (switch-to-buffer buf)
8614             (gnus-set-global-variables)
8615             (gnus-configure-windows 'summary)
8616             (gnus-message 3 "Article couldn't be entered?"))
8617         (kill-buffer dig)))))
8618
8619 (defun gnus-summary-read-document (n)
8620   "Open a new group based on the current article(s).
8621 This will allow you to read digests and other similar
8622 documents as newsgroups.
8623 Obeys the standard process/prefix convention."
8624   (interactive "P")
8625   (let* ((ogroup gnus-newsgroup-name)
8626          (params (append (gnus-info-params (gnus-get-info ogroup))
8627                          (list (cons 'to-group ogroup))))
8628          group egroup groups vgroup)
8629     (dolist (article (gnus-summary-work-articles n))
8630       (setq group (format "%s-%d" gnus-newsgroup-name article))
8631       (gnus-summary-remove-process-mark article)
8632       (when (gnus-summary-display-article article)
8633         (save-excursion
8634           (with-temp-buffer
8635             (insert-buffer-substring gnus-original-article-buffer)
8636             ;; Remove some headers that may lead nndoc to make
8637             ;; the wrong guess.
8638             (message-narrow-to-head)
8639             (goto-char (point-min))
8640             (delete-matching-lines "^Path:\\|^From ")
8641             (widen)
8642             (if (setq egroup
8643                       (gnus-group-read-ephemeral-group
8644                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8645                                      (nndoc-article-type guess))
8646                        t nil t))
8647                 (progn
8648             ;; Make all postings to this group go to the parent group.
8649                   (nconc (gnus-info-params (gnus-get-info egroup))
8650                          params)
8651                   (push egroup groups))
8652               ;; Couldn't select this doc group.
8653               (gnus-error 3 "Article couldn't be entered"))))))
8654     ;; Now we have selected all the documents.
8655     (cond
8656      ((not groups)
8657       (error "None of the articles could be interpreted as documents"))
8658      ((gnus-group-read-ephemeral-group
8659        (setq vgroup (format
8660                      "nnvirtual:%s-%s" gnus-newsgroup-name
8661                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8662        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8663        t
8664        (cons (current-buffer) 'summary)))
8665      (t
8666       (error "Couldn't select virtual nndoc group")))))
8667
8668 (defun gnus-summary-isearch-article (&optional regexp-p)
8669   "Do incremental search forward on the current article.
8670 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8671   (interactive "P")
8672   (gnus-summary-select-article)
8673   (gnus-configure-windows 'article)
8674   (gnus-eval-in-buffer-window gnus-article-buffer
8675     (save-restriction
8676       (widen)
8677       (isearch-forward regexp-p))))
8678
8679 (defun gnus-summary-search-article-forward (regexp &optional backward)
8680   "Search for an article containing REGEXP forward.
8681 If BACKWARD, search backward instead."
8682   (interactive
8683    (list (read-string
8684           (format "Search article %s (regexp%s): "
8685                   (if current-prefix-arg "backward" "forward")
8686                   (if gnus-last-search-regexp
8687                       (concat ", default " gnus-last-search-regexp)
8688                     "")))
8689          current-prefix-arg))
8690   (if (string-equal regexp "")
8691       (setq regexp (or gnus-last-search-regexp ""))
8692     (setq gnus-last-search-regexp regexp)
8693     (setq gnus-article-before-search gnus-current-article))
8694   ;; Intentionally set gnus-last-article.
8695   (setq gnus-last-article gnus-article-before-search)
8696   (let ((gnus-last-article gnus-last-article))
8697     (if (gnus-summary-search-article regexp backward)
8698         (gnus-summary-show-thread)
8699       (signal 'search-failed (list regexp)))))
8700
8701 (defun gnus-summary-search-article-backward (regexp)
8702   "Search for an article containing REGEXP backward."
8703   (interactive
8704    (list (read-string
8705           (format "Search article backward (regexp%s): "
8706                   (if gnus-last-search-regexp
8707                       (concat ", default " gnus-last-search-regexp)
8708                     "")))))
8709   (gnus-summary-search-article-forward regexp 'backward))
8710
8711 (defun gnus-summary-search-article (regexp &optional backward)
8712   "Search for an article containing REGEXP.
8713 Optional argument BACKWARD means do search for backward.
8714 `gnus-select-article-hook' is not called during the search."
8715   ;; We have to require this here to make sure that the following
8716   ;; dynamic binding isn't shadowed by autoloading.
8717   (require 'gnus-async)
8718   (require 'gnus-art)
8719   (let ((gnus-select-article-hook nil)  ;Disable hook.
8720         (gnus-article-prepare-hook nil)
8721         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8722         (gnus-use-article-prefetch nil)
8723         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8724         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8725         (gnus-visual nil)
8726         (gnus-keep-backlog nil)
8727         (gnus-break-pages nil)
8728         (gnus-summary-display-arrow nil)
8729         (gnus-updated-mode-lines nil)
8730         (gnus-auto-center-summary nil)
8731         (sum (current-buffer))
8732         (gnus-display-mime-function nil)
8733         (found nil)
8734         point)
8735     (gnus-save-hidden-threads
8736       (gnus-summary-select-article)
8737       (set-buffer gnus-article-buffer)
8738       (goto-char (window-point (get-buffer-window (current-buffer))))
8739       (when backward
8740         (forward-line -1))
8741       (while (not found)
8742         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8743         (if (if backward
8744                 (re-search-backward regexp nil t)
8745               (re-search-forward regexp nil t))
8746             ;; We found the regexp.
8747             (progn
8748               (setq found 'found)
8749               (beginning-of-line)
8750               (set-window-start
8751                (get-buffer-window (current-buffer))
8752                (point))
8753               (forward-line 1)
8754               (set-window-point
8755                (get-buffer-window (current-buffer))
8756                (point))
8757               (set-buffer sum)
8758               (setq point (point)))
8759           ;; We didn't find it, so we go to the next article.
8760           (set-buffer sum)
8761           (setq found 'not)
8762           (while (eq found 'not)
8763             (if (not (if backward (gnus-summary-find-prev)
8764                        (gnus-summary-find-next)))
8765                 ;; No more articles.
8766                 (setq found t)
8767               ;; Select the next article and adjust point.
8768               (unless (gnus-summary-article-sparse-p
8769                        (gnus-summary-article-number))
8770                 (setq found nil)
8771                 (gnus-summary-select-article)
8772                 (set-buffer gnus-article-buffer)
8773                 (widen)
8774                 (goto-char (if backward (point-max) (point-min))))))))
8775       (gnus-message 7 ""))
8776     ;; Return whether we found the regexp.
8777     (when (eq found 'found)
8778       (goto-char point)
8779       (gnus-summary-show-thread)
8780       (gnus-summary-goto-subject gnus-current-article)
8781       (gnus-summary-position-point)
8782       t)))
8783
8784 (defun gnus-find-matching-articles (header regexp)
8785   "Return a list of all articles that match REGEXP on HEADER.
8786 This search includes all articles in the current group that Gnus has
8787 fetched headers for, whether they are displayed or not."
8788   (let ((articles nil)
8789         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8790         (case-fold-search t))
8791     (dolist (header gnus-newsgroup-headers)
8792       (when (string-match regexp (funcall func header))
8793         (push (mail-header-number header) articles)))
8794     (nreverse articles)))
8795
8796 (defun gnus-summary-find-matching (header regexp &optional backward unread
8797                                           not-case-fold not-matching)
8798   "Return a list of all articles that match REGEXP on HEADER.
8799 The search stars on the current article and goes forwards unless
8800 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8801 If UNREAD is non-nil, only unread articles will
8802 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8803 in the comparisons. If NOT-MATCHING, return a list of all articles that
8804 not match REGEXP on HEADER."
8805   (let ((case-fold-search (not not-case-fold))
8806         articles d func)
8807     (if (consp header)
8808         (if (eq (car header) 'extra)
8809             (setq func
8810                   `(lambda (h)
8811                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8812                          "")))
8813           (error "%s is an invalid header" header))
8814       (unless (fboundp (intern (concat "mail-header-" header)))
8815         (error "%s is not a valid header" header))
8816       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8817     (dolist (d (if (eq backward 'all)
8818                    gnus-newsgroup-data
8819                  (gnus-data-find-list
8820                   (gnus-summary-article-number)
8821                   (gnus-data-list backward))))
8822       (when (and (or (not unread)       ; We want all articles...
8823                      (gnus-data-unread-p d)) ; Or just unreads.
8824                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8825                  (if not-matching
8826                      (not (string-match
8827                            regexp
8828                            (funcall func (gnus-data-header d))))
8829                    (string-match regexp
8830                                  (funcall func (gnus-data-header d)))))
8831         (push (gnus-data-number d) articles))) ; Success!
8832     (nreverse articles)))
8833
8834 (defun gnus-summary-execute-command (header regexp command &optional backward)
8835   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8836 If HEADER is an empty string (or nil), the match is done on the entire
8837 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8838   (interactive
8839    (list (let ((completion-ignore-case t))
8840            (completing-read
8841             "Header name: "
8842             (mapcar (lambda (header) (list (format "%s" header)))
8843                     (append
8844                      '("Number" "Subject" "From" "Lines" "Date"
8845                        "Message-ID" "Xref" "References" "Body")
8846                      gnus-extra-headers))
8847             nil 'require-match))
8848          (read-string "Regexp: ")
8849          (read-key-sequence "Command: ")
8850          current-prefix-arg))
8851   (when (equal header "Body")
8852     (setq header ""))
8853   ;; Hidden thread subtrees must be searched as well.
8854   (gnus-summary-show-all-threads)
8855   ;; We don't want to change current point nor window configuration.
8856   (save-excursion
8857     (save-window-excursion
8858       (let (gnus-visual
8859             gnus-treat-strip-trailing-blank-lines
8860             gnus-treat-strip-leading-blank-lines
8861             gnus-treat-strip-multiple-blank-lines
8862             gnus-treat-hide-boring-headers
8863             gnus-treat-fold-newsgroups
8864             gnus-article-prepare-hook)
8865         (gnus-message 6 "Executing %s..." (key-description command))
8866         ;; We'd like to execute COMMAND interactively so as to give arguments.
8867         (gnus-execute header regexp
8868                       `(call-interactively ',(key-binding command))
8869                       backward)
8870         (gnus-message 6 "Executing %s...done" (key-description command))))))
8871
8872 (defun gnus-summary-beginning-of-article ()
8873   "Scroll the article back to the beginning."
8874   (interactive)
8875   (gnus-summary-select-article)
8876   (gnus-configure-windows 'article)
8877   (gnus-eval-in-buffer-window gnus-article-buffer
8878     (widen)
8879     (goto-char (point-min))
8880     (when gnus-break-pages
8881       (gnus-narrow-to-page))))
8882
8883 (defun gnus-summary-end-of-article ()
8884   "Scroll to the end of the article."
8885   (interactive)
8886   (gnus-summary-select-article)
8887   (gnus-configure-windows 'article)
8888   (gnus-eval-in-buffer-window gnus-article-buffer
8889     (widen)
8890     (goto-char (point-max))
8891     (recenter -3)
8892     (when gnus-break-pages
8893       (when (re-search-backward page-delimiter nil t)
8894         (narrow-to-region (match-end 0) (point-max)))
8895       (gnus-narrow-to-page))))
8896
8897 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8898   "Truncate to LEN and quote all \"(\"'s in STRING."
8899   (gnus-replace-in-string (if (and len (> (length string) len))
8900                               (substring string 0 len)
8901                             string)
8902                           "[()]" "\\\\\\&"))
8903
8904 (defun gnus-summary-print-article (&optional filename n)
8905   "Generate and print a PostScript image of the process-marked (mail) articles.
8906
8907 If used interactively, print the current article if none are
8908 process-marked.  With prefix arg, prompt the user for the name of the
8909 file to save in.
8910
8911 When used from Lisp, accept two optional args FILENAME and N.  N means
8912 to print the next N articles.  If N is negative, print the N previous
8913 articles.  If N is nil and articles have been marked with the process
8914 mark, print these instead.
8915
8916 If the optional first argument FILENAME is nil, send the image to the
8917 printer.  If FILENAME is a string, save the PostScript image in a file with
8918 that name.  If FILENAME is a number, prompt the user for the name of the file
8919 to save in."
8920   (interactive (list (ps-print-preprint current-prefix-arg)))
8921   (dolist (article (gnus-summary-work-articles n))
8922     (gnus-summary-select-article nil nil 'pseudo article)
8923     (gnus-eval-in-buffer-window gnus-article-buffer
8924       (gnus-print-buffer))
8925     (gnus-summary-remove-process-mark article))
8926   (ps-despool filename))
8927
8928 (defun gnus-print-buffer ()
8929   (let ((buffer (generate-new-buffer " *print*")))
8930     (unwind-protect
8931         (progn
8932           (copy-to-buffer buffer (point-min) (point-max))
8933           (set-buffer buffer)
8934           (gnus-remove-text-with-property 'gnus-decoration)
8935           (when (gnus-visual-p 'article-highlight 'highlight)
8936             ;; Copy-to-buffer doesn't copy overlay.  So redo
8937             ;; highlight.
8938             (let ((gnus-article-buffer buffer))
8939               (gnus-article-highlight-citation t)
8940               (gnus-article-highlight-signature)
8941               (gnus-article-emphasize)
8942               (gnus-article-delete-invisible-text)))
8943           (let ((ps-left-header
8944                  (list
8945                   (concat "("
8946                           (gnus-summary-print-truncate-and-quote
8947                            (mail-header-subject gnus-current-headers)
8948                            66) ")")
8949                   (concat "("
8950                           (gnus-summary-print-truncate-and-quote
8951                            (mail-header-from gnus-current-headers)
8952                            45) ")")))
8953                 (ps-right-header
8954                  (list
8955                   "/pagenumberstring load"
8956                   (concat "("
8957                           (mail-header-date gnus-current-headers) ")"))))
8958             (gnus-run-hooks 'gnus-ps-print-hook)
8959             (save-excursion
8960               (if window-system
8961                   (ps-spool-buffer-with-faces)
8962                 (ps-spool-buffer)))))
8963       (kill-buffer buffer))))
8964
8965 (defun gnus-summary-show-article (&optional arg)
8966   "Force redisplaying of the current article.
8967 If ARG (the prefix) is a number, show the article with the charset
8968 defined in `gnus-summary-show-article-charset-alist', or the charset
8969 input.
8970 If ARG (the prefix) is non-nil and not a number, show the raw article
8971 without any article massaging functions being run.  Normally, the key
8972 strokes are `C-u g'."
8973   (interactive "P")
8974   (cond
8975    ((numberp arg)
8976     (gnus-summary-show-article t)
8977     (let ((gnus-newsgroup-charset
8978            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8979                (mm-read-coding-system
8980                 "View as charset: " ;; actually it is coding system.
8981                 (with-current-buffer gnus-article-buffer
8982                   (mm-detect-coding-region (point) (point-max))))))
8983           (gnus-newsgroup-ignored-charsets 'gnus-all))
8984       (gnus-summary-select-article nil 'force)
8985       (let ((deps gnus-newsgroup-dependencies)
8986             head header lines)
8987         (save-excursion
8988           (set-buffer gnus-original-article-buffer)
8989           (save-restriction
8990             (message-narrow-to-head)
8991             (setq head (buffer-string))
8992             (goto-char (point-min))
8993             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8994               (goto-char (point-max))
8995               (widen)
8996               (setq lines (1- (count-lines (point) (point-max))))))
8997           (with-temp-buffer
8998             (insert (format "211 %d Article retrieved.\n"
8999                             (cdr gnus-article-current)))
9000             (insert head)
9001             (if lines (insert (format "Lines: %d\n" lines)))
9002             (insert ".\n")
9003             (let ((nntp-server-buffer (current-buffer)))
9004               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9005         (gnus-data-set-header
9006          (gnus-data-find (cdr gnus-article-current))
9007          header)
9008         (gnus-summary-update-article-line
9009          (cdr gnus-article-current) header)
9010         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9011           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9012    ((not arg)
9013     ;; Select the article the normal way.
9014     (gnus-summary-select-article nil 'force))
9015    (t
9016     ;; We have to require this here to make sure that the following
9017     ;; dynamic binding isn't shadowed by autoloading.
9018     (require 'gnus-async)
9019     (require 'gnus-art)
9020     ;; Bind the article treatment functions to nil.
9021     (let ((gnus-have-all-headers t)
9022           gnus-article-prepare-hook
9023           gnus-article-decode-hook
9024           gnus-display-mime-function
9025           gnus-break-pages)
9026       ;; Destroy any MIME parts.
9027       (when (gnus-buffer-live-p gnus-article-buffer)
9028         (save-excursion
9029           (set-buffer gnus-article-buffer)
9030           (mm-destroy-parts gnus-article-mime-handles)
9031           ;; Set it to nil for safety reason.
9032           (setq gnus-article-mime-handle-alist nil)
9033           (setq gnus-article-mime-handles nil)))
9034       (gnus-summary-select-article nil 'force))))
9035   (gnus-summary-goto-subject gnus-current-article)
9036   (gnus-summary-position-point))
9037
9038 (defun gnus-summary-show-raw-article ()
9039   "Show the raw article without any article massaging functions being run."
9040   (interactive)
9041   (gnus-summary-show-article t))
9042
9043 (defun gnus-summary-verbose-headers (&optional arg)
9044   "Toggle permanent full header display.
9045 If ARG is a positive number, turn header display on.
9046 If ARG is a negative number, turn header display off."
9047   (interactive "P")
9048   (setq gnus-show-all-headers
9049         (cond ((or (not (numberp arg))
9050                    (zerop arg))
9051                (not gnus-show-all-headers))
9052               ((natnump arg)
9053                t)))
9054   (gnus-summary-show-article))
9055
9056 (defun gnus-summary-toggle-header (&optional arg)
9057   "Show the headers if they are hidden, or hide them if they are shown.
9058 If ARG is a positive number, show the entire header.
9059 If ARG is a negative number, hide the unwanted header lines."
9060   (interactive "P")
9061   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9062                      (get-buffer-window gnus-article-buffer t))))
9063     (with-current-buffer gnus-article-buffer
9064       (widen)
9065       (article-narrow-to-head)
9066       (let* ((buffer-read-only nil)
9067              (inhibit-point-motion-hooks t)
9068              (hidden (if (numberp arg)
9069                          (>= arg 0)
9070                        (or (not (looking-at "[^ \t\n]+:"))
9071                            (gnus-article-hidden-text-p 'headers))))
9072              s e)
9073         (delete-region (point-min) (point-max))
9074         (with-current-buffer gnus-original-article-buffer
9075           (goto-char (setq s (point-min)))
9076           (setq e (if (search-forward "\n\n" nil t)
9077                       (1- (point))
9078                     (point-max))))
9079         (insert-buffer-substring gnus-original-article-buffer s e)
9080         (run-hooks 'gnus-article-decode-hook)
9081         (if hidden
9082             (let ((gnus-treat-hide-headers nil)
9083                   (gnus-treat-hide-boring-headers nil))
9084               (gnus-delete-wash-type 'headers)
9085               (gnus-treat-article 'head))
9086           (gnus-treat-article 'head))
9087         (widen)
9088         (if window
9089             (set-window-start window (goto-char (point-min))))
9090         (if gnus-break-pages
9091             (gnus-narrow-to-page)
9092           (when (gnus-visual-p 'page-marker)
9093             (let ((buffer-read-only nil))
9094               (gnus-remove-text-with-property 'gnus-prev)
9095               (gnus-remove-text-with-property 'gnus-next))))
9096         (gnus-set-mode-line 'article)))))
9097
9098 (defun gnus-summary-show-all-headers ()
9099   "Make all header lines visible."
9100   (interactive)
9101   (gnus-summary-toggle-header 1))
9102
9103 (defun gnus-summary-caesar-message (&optional arg)
9104   "Caesar rotate the current article by 13.
9105 The numerical prefix specifies how many places to rotate each letter
9106 forward."
9107   (interactive "P")
9108   (gnus-summary-select-article)
9109   (let ((mail-header-separator ""))
9110     (gnus-eval-in-buffer-window gnus-article-buffer
9111       (save-restriction
9112         (widen)
9113         (let ((start (window-start))
9114               buffer-read-only)
9115           (message-caesar-buffer-body arg)
9116           (set-window-start (get-buffer-window (current-buffer)) start)))))
9117   ;; Create buttons and stuff...
9118   (gnus-treat-article nil))
9119
9120 (defun gnus-summary-idna-message (&optional arg)
9121   "Decode IDNA encoded domain names in the current articles.
9122 IDNA encoded domain names looks like `xn--bar'.  If a string
9123 remain unencoded after running this function, it is likely an
9124 invalid IDNA string (`xn--bar' is invalid).
9125
9126 You must have GNU Libidn (`http://www.gnu.org/software/libidn/')
9127 installed for this command to work."
9128   (interactive "P")
9129   (if (not (and (condition-case nil (require 'idna)
9130                   (file-error))
9131                 (mm-coding-system-p 'utf-8)
9132                 (executable-find (symbol-value 'idna-program))))
9133       (gnus-message
9134        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9135     (gnus-summary-select-article)
9136     (let ((mail-header-separator ""))
9137       (gnus-eval-in-buffer-window gnus-article-buffer
9138         (save-restriction
9139           (widen)
9140           (let ((start (window-start))
9141                 buffer-read-only)
9142             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9143               (replace-match (idna-to-unicode (match-string 1))))
9144             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9145
9146 (autoload 'unmorse-region "morse"
9147   "Convert morse coded text in region to ordinary ASCII text."
9148   t)
9149
9150 (defun gnus-summary-morse-message (&optional arg)
9151   "Morse decode the current article."
9152   (interactive "P")
9153   (gnus-summary-select-article)
9154   (let ((mail-header-separator ""))
9155     (gnus-eval-in-buffer-window gnus-article-buffer
9156       (save-excursion
9157         (save-restriction
9158           (widen)
9159           (let ((pos (window-start))
9160                 buffer-read-only)
9161             (goto-char (point-min))
9162             (when (message-goto-body)
9163               (gnus-narrow-to-body))
9164             (goto-char (point-min))
9165             (while (search-forward "·" (point-max) t)
9166               (replace-match "."))
9167             (unmorse-region (point-min) (point-max))
9168             (widen)
9169             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9170
9171 (defun gnus-summary-stop-page-breaking ()
9172   "Stop page breaking in the current article."
9173   (interactive)
9174   (gnus-summary-select-article)
9175   (gnus-eval-in-buffer-window gnus-article-buffer
9176     (widen)
9177     (when (gnus-visual-p 'page-marker)
9178       (let ((buffer-read-only nil))
9179         (gnus-remove-text-with-property 'gnus-prev)
9180         (gnus-remove-text-with-property 'gnus-next))
9181       (setq gnus-page-broken nil))))
9182
9183 (defun gnus-summary-move-article (&optional n to-newsgroup
9184                                             select-method action)
9185   "Move the current article to a different newsgroup.
9186 If N is a positive number, move the N next articles.
9187 If N is a negative number, move the N previous articles.
9188 If N is nil and any articles have been marked with the process mark,
9189 move those articles instead.
9190 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9191 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9192 re-spool using this method.
9193
9194 When called interactively with TO-NEWSGROUP being nil, the value of
9195 the variable `gnus-move-split-methods' is used for finding a default
9196 for the target newsgroup.
9197
9198 For this function to work, both the current newsgroup and the
9199 newsgroup that you want to move to have to support the `request-move'
9200 and `request-accept' functions.
9201
9202 ACTION can be either `move' (the default), `crosspost' or `copy'."
9203   (interactive "P")
9204   (unless action
9205     (setq action 'move))
9206   ;; Check whether the source group supports the required functions.
9207   (cond ((and (eq action 'move)
9208               (not (gnus-check-backend-function
9209                     'request-move-article gnus-newsgroup-name)))
9210          (error "The current group does not support article moving"))
9211         ((and (eq action 'crosspost)
9212               (not (gnus-check-backend-function
9213                     'request-replace-article gnus-newsgroup-name)))
9214          (error "The current group does not support article editing")))
9215   (let ((articles (gnus-summary-work-articles n))
9216         (prefix (if (gnus-check-backend-function
9217                      'request-move-article gnus-newsgroup-name)
9218                     (funcall gnus-move-group-prefix-function
9219                              gnus-newsgroup-name)
9220                   ""))
9221         (names '((move "Move" "Moving")
9222                  (copy "Copy" "Copying")
9223                  (crosspost "Crosspost" "Crossposting")))
9224         (copy-buf (save-excursion
9225                     (nnheader-set-temp-buffer " *copy article*")))
9226         art-group to-method new-xref article to-groups articles-to-update-marks)
9227     (unless (assq action names)
9228       (error "Unknown action %s" action))
9229     ;; Read the newsgroup name.
9230     (when (and (not to-newsgroup)
9231                (not select-method))
9232       (if (and gnus-move-split-methods
9233                (not
9234                 (and (memq gnus-current-article articles)
9235                      (gnus-buffer-live-p gnus-original-article-buffer))))
9236           ;; When `gnus-move-split-methods' is non-nil, we have to
9237           ;; select an article to give `gnus-read-move-group-name' an
9238           ;; opportunity to suggest an appropriate default.  However,
9239           ;; we needn't render or mark the article.
9240           (let ((gnus-display-mime-function nil)
9241                 (gnus-article-prepare-hook nil)
9242                 (gnus-mark-article-hook nil))
9243             (gnus-summary-select-article nil nil nil (car articles))))
9244       (setq to-newsgroup
9245             (gnus-read-move-group-name
9246              (cadr (assq action names))
9247              (symbol-value (intern (format "gnus-current-%s-group" action)))
9248              articles prefix))
9249       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
9250     (setq to-method (or select-method
9251                         (gnus-server-to-method
9252                          (gnus-group-method to-newsgroup))))
9253     ;; Check the method we are to move this article to...
9254     (unless (gnus-check-backend-function
9255              'request-accept-article (car to-method))
9256       (error "%s does not support article copying" (car to-method)))
9257     (unless (gnus-check-server to-method)
9258       (error "Can't open server %s" (car to-method)))
9259     (gnus-message 6 "%s to %s: %s..."
9260                   (caddr (assq action names))
9261                   (or (car select-method) to-newsgroup) articles)
9262     (while articles
9263       (setq article (pop articles))
9264       (setq
9265        art-group
9266        (cond
9267         ;; Move the article.
9268         ((eq action 'move)
9269          ;; Remove this article from future suppression.
9270          (gnus-dup-unsuppress-article article)
9271          (let* ((from-method (gnus-find-method-for-group
9272                               gnus-newsgroup-name))
9273                 (to-method (gnus-find-method-for-group
9274                             to-newsgroup))
9275                 (move-is-internal (gnus-method-equal from-method to-method)))
9276          (gnus-request-move-article
9277           article                       ; Article to move
9278           gnus-newsgroup-name           ; From newsgroup
9279           (nth 1 (gnus-find-method-for-group
9280                   gnus-newsgroup-name)) ; Server
9281           (list 'gnus-request-accept-article
9282                 to-newsgroup (list 'quote select-method)
9283                 (not articles) t)       ; Accept form
9284           (not articles)                ; Only save nov last time
9285           move-is-internal)))           ; is this move internal?
9286         ;; Copy the article.
9287         ((eq action 'copy)
9288          (save-excursion
9289            (set-buffer copy-buf)
9290            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9291              (save-restriction
9292                (nnheader-narrow-to-headers)
9293                (dolist (hdr gnus-copy-article-ignored-headers)
9294                  (message-remove-header hdr t)))
9295              (gnus-request-accept-article
9296               to-newsgroup select-method (not articles) t))))
9297         ;; Crosspost the article.
9298         ((eq action 'crosspost)
9299          (let ((xref (message-tokenize-header
9300                       (mail-header-xref (gnus-summary-article-header article))
9301                       " ")))
9302            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9303                                   ":" (number-to-string article)))
9304            (unless xref
9305              (setq xref (list (system-name))))
9306            (setq new-xref
9307                  (concat
9308                   (mapconcat 'identity
9309                              (delete "Xref:" (delete new-xref xref))
9310                              " ")
9311                   " " new-xref))
9312            (save-excursion
9313              (set-buffer copy-buf)
9314              ;; First put the article in the destination group.
9315              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9316              (when (consp (setq art-group
9317                                 (gnus-request-accept-article
9318                                  to-newsgroup select-method (not articles))))
9319                (setq new-xref (concat new-xref " " (car art-group)
9320                                       ":"
9321                                       (number-to-string (cdr art-group))))
9322                ;; Now we have the new Xrefs header, so we insert
9323                ;; it and replace the new article.
9324                (nnheader-replace-header "Xref" new-xref)
9325                (gnus-request-replace-article
9326                 (cdr art-group) to-newsgroup (current-buffer))
9327                art-group))))))
9328       (cond
9329        ((not art-group)
9330         (gnus-message 1 "Couldn't %s article %s: %s"
9331                       (cadr (assq action names)) article
9332                       (nnheader-get-report (car to-method))))
9333        ((eq art-group 'junk)
9334         (when (eq action 'move)
9335           (gnus-summary-mark-article article gnus-canceled-mark)
9336           (gnus-message 4 "Deleted article %s" article)
9337           ;; run the delete hook
9338           (run-hook-with-args 'gnus-summary-article-delete-hook
9339                               action
9340                               (gnus-data-header
9341                                (assoc article (gnus-data-list nil)))
9342                               gnus-newsgroup-name nil
9343                               select-method)))
9344        (t
9345         (let* ((pto-group (gnus-group-prefixed-name
9346                            (car art-group) to-method))
9347                (info (gnus-get-info pto-group))
9348                (to-group (gnus-info-group info))
9349                to-marks)
9350           ;; Update the group that has been moved to.
9351           (when (and info
9352                      (memq action '(move copy)))
9353             (unless (member to-group to-groups)
9354               (push to-group to-groups))
9355
9356             (unless (memq article gnus-newsgroup-unreads)
9357               (push 'read to-marks)
9358               (gnus-info-set-read
9359                info (gnus-add-to-range (gnus-info-read info)
9360                                        (list (cdr art-group)))))
9361
9362             ;; See whether the article is to be put in the cache.
9363             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9364                              gnus-article-mark-lists
9365                            (delete '(expirable . expire)
9366                                    (copy-sequence gnus-article-mark-lists))))
9367                   (to-article (cdr art-group)))
9368
9369               ;; Enter the article into the cache in the new group,
9370               ;; if that is required.
9371               (when gnus-use-cache
9372                 (gnus-cache-possibly-enter-article
9373                  to-group to-article
9374                  (memq article gnus-newsgroup-marked)
9375                  (memq article gnus-newsgroup-dormant)
9376                  (memq article gnus-newsgroup-unreads)))
9377
9378               (when gnus-preserve-marks
9379                 ;; Copy any marks over to the new group.
9380                 (when (and (equal to-group gnus-newsgroup-name)
9381                            (not (memq article gnus-newsgroup-unreads)))
9382                   ;; Mark this article as read in this group.
9383                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9384                   (setcdr (gnus-active to-group) to-article)
9385                   (setcdr gnus-newsgroup-active to-article))
9386
9387                 (while marks
9388                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9389                     (when (memq article (symbol-value
9390                                          (intern (format "gnus-newsgroup-%s"
9391                                                          (caar marks)))))
9392                       (push (cdar marks) to-marks)
9393                       ;; If the other group is the same as this group,
9394                       ;; then we have to add the mark to the list.
9395                       (when (equal to-group gnus-newsgroup-name)
9396                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9397                              (cons to-article
9398                                    (symbol-value
9399                                     (intern (format "gnus-newsgroup-%s"
9400                                                     (caar marks)))))))
9401                       ;; Copy the marks to other group.
9402                       (gnus-add-marked-articles
9403                        to-group (cdar marks) (list to-article) info)))
9404                   (setq marks (cdr marks)))
9405
9406                 (gnus-request-set-mark
9407                  to-group (list (list (list to-article) 'add to-marks))))
9408
9409               (gnus-dribble-enter
9410                (concat "(gnus-group-set-info '"
9411                        (gnus-prin1-to-string (gnus-get-info to-group))
9412                        ")"))))
9413
9414           ;; Update the Xref header in this article to point to
9415           ;; the new crossposted article we have just created.
9416           (when (eq action 'crosspost)
9417             (save-excursion
9418               (set-buffer copy-buf)
9419               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9420               (nnheader-replace-header "Xref" new-xref)
9421               (gnus-request-replace-article
9422                article gnus-newsgroup-name (current-buffer))))
9423
9424           ;; run the move/copy/crosspost/respool hook
9425           (run-hook-with-args 'gnus-summary-article-move-hook
9426                               action
9427                               (gnus-data-header
9428                                (assoc article (gnus-data-list nil)))
9429                               gnus-newsgroup-name
9430                               to-newsgroup
9431                               select-method))
9432
9433         ;;;!!!Why is this necessary?
9434         (set-buffer gnus-summary-buffer)
9435         
9436         (gnus-summary-goto-subject article)
9437         (when (eq action 'move)
9438           (gnus-summary-mark-article article gnus-canceled-mark))))
9439       (push article articles-to-update-marks))
9440
9441     (apply 'gnus-summary-remove-process-mark articles-to-update-marks)
9442     ;; Re-activate all groups that have been moved to.
9443     (save-excursion
9444       (set-buffer gnus-group-buffer)
9445       (let ((gnus-group-marked to-groups))
9446         (gnus-group-get-new-news-this-group nil t)))
9447     
9448     (gnus-kill-buffer copy-buf)
9449     (gnus-summary-position-point)
9450     (gnus-set-mode-line 'summary)))
9451
9452 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9453   "Copy the current article to some other group.
9454 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9455 When called interactively, if TO-NEWSGROUP is nil, use the value of
9456 the variable `gnus-move-split-methods' for finding a default target
9457 newsgroup.
9458 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9459 re-spool using this method."
9460   (interactive "P")
9461   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9462
9463 (defun gnus-summary-crosspost-article (&optional n)
9464   "Crosspost the current article to some other group."
9465   (interactive "P")
9466   (gnus-summary-move-article n nil nil 'crosspost))
9467
9468 (defcustom gnus-summary-respool-default-method nil
9469   "Default method type for respooling an article.
9470 If nil, use to the current newsgroup method."
9471   :type 'symbol
9472   :group 'gnus-summary-mail)
9473
9474 (defcustom gnus-summary-display-while-building nil
9475   "If non-nil, show and update the summary buffer as it's being built.
9476 If the value is t, update the buffer after every line is inserted.  If
9477 the value is an integer (N), update the display every N lines."
9478   :version "22.1"
9479   :group 'gnus-thread
9480   :type '(choice (const :tag "off" nil)
9481                  number
9482                  (const :tag "frequently" t)))
9483
9484 (defun gnus-summary-respool-article (&optional n method)
9485   "Respool the current article.
9486 The article will be squeezed through the mail spooling process again,
9487 which means that it will be put in some mail newsgroup or other
9488 depending on `nnmail-split-methods'.
9489 If N is a positive number, respool the N next articles.
9490 If N is a negative number, respool the N previous articles.
9491 If N is nil and any articles have been marked with the process mark,
9492 respool those articles instead.
9493
9494 Respooling can be done both from mail groups and \"real\" newsgroups.
9495 In the former case, the articles in question will be moved from the
9496 current group into whatever groups they are destined to.  In the
9497 latter case, they will be copied into the relevant groups."
9498   (interactive
9499    (list current-prefix-arg
9500          (let* ((methods (gnus-methods-using 'respool))
9501                 (methname
9502                  (symbol-name (or gnus-summary-respool-default-method
9503                                   (car (gnus-find-method-for-group
9504                                         gnus-newsgroup-name)))))
9505                 (method
9506                  (gnus-completing-read-with-default
9507                   methname "Backend to use when respooling"
9508                   methods nil t nil 'gnus-mail-method-history))
9509                 ms)
9510            (cond
9511             ((zerop (length (setq ms (gnus-servers-using-backend
9512                                       (intern method)))))
9513              (list (intern method) ""))
9514             ((= 1 (length ms))
9515              (car ms))
9516             (t
9517              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9518                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9519                            ms-alist))))))))
9520   (unless method
9521     (error "No method given for respooling"))
9522   (if (assoc (symbol-name
9523               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9524              (gnus-methods-using 'respool))
9525       (gnus-summary-move-article n nil method)
9526     (gnus-summary-copy-article n nil method)))
9527
9528 (defun gnus-summary-import-article (file &optional edit)
9529   "Import an arbitrary file into a mail newsgroup."
9530   (interactive "fImport file: \nP")
9531   (let ((group gnus-newsgroup-name)
9532         (now (current-time))
9533         atts lines group-art)
9534     (unless (gnus-check-backend-function 'request-accept-article group)
9535       (error "%s does not support article importing" group))
9536     (or (file-readable-p file)
9537         (not (file-regular-p file))
9538         (error "Can't read %s" file))
9539     (save-excursion
9540       (set-buffer (gnus-get-buffer-create " *import file*"))
9541       (erase-buffer)
9542       (nnheader-insert-file-contents file)
9543       (goto-char (point-min))
9544       (if (nnheader-article-p)
9545           (save-restriction
9546             (goto-char (point-min))
9547             (search-forward "\n\n" nil t)
9548             (narrow-to-region (point-min) (1- (point)))
9549             (goto-char (point-min))
9550             (unless (re-search-forward "^date:" nil t)
9551               (goto-char (point-max))
9552               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9553        ;; This doesn't look like an article, so we fudge some headers.
9554         (setq atts (file-attributes file)
9555               lines (count-lines (point-min) (point-max)))
9556         (insert "From: " (read-string "From: ") "\n"
9557                 "Subject: " (read-string "Subject: ") "\n"
9558                 "Date: " (message-make-date (nth 5 atts)) "\n"
9559                 "Message-ID: " (message-make-message-id) "\n"
9560                 "Lines: " (int-to-string lines) "\n"
9561                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9562       (setq group-art (gnus-request-accept-article group nil t))
9563       (kill-buffer (current-buffer)))
9564     (setq gnus-newsgroup-active (gnus-activate-group group))
9565     (forward-line 1)
9566     (gnus-summary-goto-article (cdr group-art) nil t)
9567     (when edit
9568       (gnus-summary-edit-article))))
9569
9570 (defun gnus-summary-create-article ()
9571   "Create an article in a mail newsgroup."
9572   (interactive)
9573   (let ((group gnus-newsgroup-name)
9574         (now (current-time))
9575         group-art)
9576     (unless (gnus-check-backend-function 'request-accept-article group)
9577       (error "%s does not support article importing" group))
9578     (save-excursion
9579       (set-buffer (gnus-get-buffer-create " *import file*"))
9580       (erase-buffer)
9581       (goto-char (point-min))
9582       ;; This doesn't look like an article, so we fudge some headers.
9583       (insert "From: " (read-string "From: ") "\n"
9584               "Subject: " (read-string "Subject: ") "\n"
9585               "Date: " (message-make-date now) "\n"
9586               "Message-ID: " (message-make-message-id) "\n")
9587       (setq group-art (gnus-request-accept-article group nil t))
9588       (kill-buffer (current-buffer)))
9589     (setq gnus-newsgroup-active (gnus-activate-group group))
9590     (forward-line 1)
9591     (gnus-summary-goto-article (cdr group-art) nil t)
9592     (gnus-summary-edit-article)))
9593
9594 (defun gnus-summary-article-posted-p ()
9595   "Say whether the current (mail) article is available from news as well.
9596 This will be the case if the article has both been mailed and posted."
9597   (interactive)
9598   (let ((id (mail-header-references (gnus-summary-article-header)))
9599         (gnus-override-method (car (gnus-refer-article-methods))))
9600     (if (gnus-request-head id "")
9601         (gnus-message 2 "The current message was found on %s"
9602                       gnus-override-method)
9603       (gnus-message 2 "The current message couldn't be found on %s"
9604                     gnus-override-method)
9605       nil)))
9606
9607 (defun gnus-summary-expire-articles (&optional now)
9608   "Expire all articles that are marked as expirable in the current group."
9609   (interactive)
9610   (when (and (not gnus-group-is-exiting-without-update-p)
9611              (gnus-check-backend-function
9612               'request-expire-articles gnus-newsgroup-name))
9613     ;; This backend supports expiry.
9614     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9615            (expirable (if total
9616                           (progn
9617                             ;; We need to update the info for
9618                             ;; this group for `gnus-list-of-read-articles'
9619                             ;; to give us the right answer.
9620                             (gnus-run-hooks 'gnus-exit-group-hook)
9621                             (gnus-summary-update-info)
9622                             (gnus-list-of-read-articles gnus-newsgroup-name))
9623                         (setq gnus-newsgroup-expirable
9624                               (sort gnus-newsgroup-expirable '<))))
9625            (expiry-wait (if now 'immediate
9626                           (gnus-group-find-parameter
9627                            gnus-newsgroup-name 'expiry-wait)))
9628            (nnmail-expiry-target
9629             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9630                 nnmail-expiry-target))
9631            es)
9632       (when expirable
9633         ;; There are expirable articles in this group, so we run them
9634         ;; through the expiry process.
9635         (gnus-message 6 "Expiring articles...")
9636         (unless (gnus-check-group gnus-newsgroup-name)
9637           (error "Can't open server for %s" gnus-newsgroup-name))
9638         ;; The list of articles that weren't expired is returned.
9639         (save-excursion
9640           (if expiry-wait
9641               (let ((nnmail-expiry-wait-function nil)
9642                     (nnmail-expiry-wait expiry-wait))
9643                 (setq es (gnus-request-expire-articles
9644                           expirable gnus-newsgroup-name)))
9645             (setq es (gnus-request-expire-articles
9646                       expirable gnus-newsgroup-name)))
9647           (unless total
9648             (setq gnus-newsgroup-expirable es))
9649           ;; We go through the old list of expirable, and mark all
9650           ;; really expired articles as nonexistent.
9651           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9652             (let ((gnus-use-cache nil))
9653               (dolist (article expirable)
9654                 (when (and (not (memq article es))
9655                            (gnus-data-find article))
9656                   (gnus-summary-mark-article article gnus-canceled-mark)
9657                   (run-hook-with-args 'gnus-summary-article-expire-hook
9658                                       'delete
9659                                       (gnus-data-header
9660                                        (assoc article (gnus-data-list nil)))
9661                                       gnus-newsgroup-name
9662                                       nil
9663                                       nil))))))
9664         (gnus-message 6 "Expiring articles...done")))))
9665
9666 (defun gnus-summary-expire-articles-now ()
9667   "Expunge all expirable articles in the current group.
9668 This means that *all* articles that are marked as expirable will be
9669 deleted forever, right now."
9670   (interactive)
9671   (or gnus-expert-user
9672       (gnus-yes-or-no-p
9673        "Are you really, really, really sure you want to delete all these messages? ")
9674       (error "Phew!"))
9675   (gnus-summary-expire-articles t))
9676
9677 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9678 (defun gnus-summary-delete-article (&optional n)
9679   "Delete the N next (mail) articles.
9680 This command actually deletes articles.  This is not a marking
9681 command.  The article will disappear forever from your life, never to
9682 return.
9683
9684 If N is negative, delete backwards.
9685 If N is nil and articles have been marked with the process mark,
9686 delete these instead.
9687
9688 If `gnus-novice-user' is non-nil you will be asked for
9689 confirmation before the articles are deleted."
9690   (interactive "P")
9691   (unless (gnus-check-backend-function 'request-expire-articles
9692                                        gnus-newsgroup-name)
9693     (error "The current newsgroup does not support article deletion"))
9694   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9695     (error "Couldn't open server"))
9696   ;; Compute the list of articles to delete.
9697   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9698         (nnmail-expiry-target 'delete)
9699         not-deleted)
9700     (if (and gnus-novice-user
9701              (not (gnus-yes-or-no-p
9702                    (format "Do you really want to delete %s forever? "
9703                            (if (> (length articles) 1)
9704                                (format "these %s articles" (length articles))
9705                              "this article")))))
9706         ()
9707       ;; Delete the articles.
9708       (setq not-deleted (gnus-request-expire-articles
9709                          articles gnus-newsgroup-name 'force))
9710       (while articles
9711         (gnus-summary-remove-process-mark (car articles))
9712         ;; The backend might not have been able to delete the article
9713         ;; after all.
9714         (unless (memq (car articles) not-deleted)
9715           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9716         (let* ((article (car articles))
9717                (ghead  (gnus-data-header
9718                                     (assoc article (gnus-data-list nil)))))
9719           (run-hook-with-args 'gnus-summary-article-delete-hook
9720                               'delete ghead gnus-newsgroup-name nil
9721                               nil))
9722         (setq articles (cdr articles)))
9723       (when not-deleted
9724         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9725     (gnus-summary-position-point)
9726     (gnus-set-mode-line 'summary)
9727     not-deleted))
9728
9729 (defun gnus-summary-edit-article (&optional arg)
9730   "Edit the current article.
9731 This will have permanent effect only in mail groups.
9732 If ARG is nil, edit the decoded articles.
9733 If ARG is 1, edit the raw articles.
9734 If ARG is 2, edit the raw articles even in read-only groups.
9735 If ARG is 3, edit the articles with the current handles.
9736 Otherwise, allow editing of articles even in read-only
9737 groups."
9738   (interactive "P")
9739   (let (force raw current-handles)
9740     (cond
9741      ((null arg))
9742      ((eq arg 1)
9743       (setq raw t))
9744      ((eq arg 2)
9745       (setq raw t
9746             force t))
9747      ((eq arg 3)
9748       (setq current-handles
9749             (and (gnus-buffer-live-p gnus-article-buffer)
9750                  (with-current-buffer gnus-article-buffer
9751                    (prog1
9752                        gnus-article-mime-handles
9753                      (setq gnus-article-mime-handles nil))))))
9754      (t
9755       (setq force t)))
9756     (when (and raw (not force)
9757                (member gnus-newsgroup-name '("nndraft:delayed"
9758                                              "nndraft:drafts"
9759                                              "nndraft:queue")))
9760       (error "Can't edit the raw article in group %s"
9761              gnus-newsgroup-name))
9762     (save-excursion
9763       (set-buffer gnus-summary-buffer)
9764       (let ((mail-parse-charset gnus-newsgroup-charset)
9765             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9766         (gnus-set-global-variables)
9767         (when (and (not force)
9768                    (gnus-group-read-only-p))
9769           (error "The current newsgroup does not support article editing"))
9770         (gnus-summary-show-article t)
9771         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9772           (with-current-buffer gnus-article-buffer
9773             (mm-enable-multibyte)))
9774         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9775             (setq raw t))
9776         (gnus-article-edit-article
9777          (if raw 'ignore
9778            `(lambda ()
9779               (let ((mbl mml-buffer-list))
9780                 (setq mml-buffer-list nil)
9781                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
9782                   (mime-to-mml ,'current-handles))
9783                 (let ((mbl1 mml-buffer-list))
9784                   (setq mml-buffer-list mbl)
9785                   (set (make-local-variable 'mml-buffer-list) mbl1))
9786                 (gnus-make-local-hook 'kill-buffer-hook)
9787                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9788          `(lambda (no-highlight)
9789             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9790                   (message-options message-options)
9791                   (message-options-set-recipient)
9792                   (mail-parse-ignored-charsets
9793                    ',gnus-newsgroup-ignored-charsets))
9794               ,(if (not raw) '(progn
9795                                 (mml-to-mime)
9796                                 (mml-destroy-buffers)
9797                                 (remove-hook 'kill-buffer-hook
9798                                              'mml-destroy-buffers t)
9799                                 (kill-local-variable 'mml-buffer-list)))
9800               (gnus-summary-edit-article-done
9801                ,(or (mail-header-references gnus-current-headers) "")
9802                ,(gnus-group-read-only-p)
9803                ,gnus-summary-buffer no-highlight))))))))
9804
9805 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9806
9807 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9808                                                  no-highlight)
9809   "Make edits to the current article permanent."
9810   (interactive)
9811   (save-excursion
9812    ;; The buffer restriction contains the entire article if it exists.
9813     (when (article-goto-body)
9814       (let ((lines (count-lines (point) (point-max)))
9815             (length (- (point-max) (point)))
9816             (case-fold-search t)
9817             (body (copy-marker (point))))
9818         (goto-char (point-min))
9819         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9820           (delete-region (match-beginning 1) (match-end 1))
9821           (insert (number-to-string length)))
9822         (goto-char (point-min))
9823         (when (re-search-forward
9824                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9825           (delete-region (match-beginning 1) (match-end 1))
9826           (insert (number-to-string length)))
9827         (goto-char (point-min))
9828         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9829           (delete-region (match-beginning 1) (match-end 1))
9830           (insert (number-to-string lines))))))
9831   ;; Replace the article.
9832   (let ((buf (current-buffer)))
9833     (with-temp-buffer
9834       (insert-buffer-substring buf)
9835
9836       (if (and (not read-only)
9837                (not (gnus-request-replace-article
9838                      (cdr gnus-article-current) (car gnus-article-current)
9839                      (current-buffer) t)))
9840           (error "Couldn't replace article")
9841         ;; Update the summary buffer.
9842         (if (and references
9843                  (equal (message-tokenize-header references " ")
9844                         (message-tokenize-header
9845                          (or (message-fetch-field "references") "") " ")))
9846             ;; We only have to update this line.
9847             (save-excursion
9848               (save-restriction
9849                 (message-narrow-to-head)
9850                 (let ((head (buffer-substring-no-properties
9851                              (point-min) (point-max)))
9852                       header)
9853                   (with-temp-buffer
9854                     (insert (format "211 %d Article retrieved.\n"
9855                                     (cdr gnus-article-current)))
9856                     (insert head)
9857                     (insert ".\n")
9858                     (let ((nntp-server-buffer (current-buffer)))
9859                       (setq header (car (gnus-get-newsgroup-headers
9860                                          nil t))))
9861                     (save-excursion
9862                       (set-buffer gnus-summary-buffer)
9863                       (gnus-data-set-header
9864                        (gnus-data-find (cdr gnus-article-current))
9865                        header)
9866                       (gnus-summary-update-article-line
9867                        (cdr gnus-article-current) header)
9868                       (if (gnus-summary-goto-subject
9869                            (cdr gnus-article-current) nil t)
9870                           (gnus-summary-update-secondary-mark
9871                            (cdr gnus-article-current))))))))
9872           ;; Update threads.
9873           (set-buffer (or buffer gnus-summary-buffer))
9874           (gnus-summary-update-article (cdr gnus-article-current))
9875           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9876               (gnus-summary-update-secondary-mark
9877                (cdr gnus-article-current))))
9878         ;; Prettify the article buffer again.
9879         (unless no-highlight
9880           (save-excursion
9881             (set-buffer gnus-article-buffer)
9882             ;;;!!! Fix this -- article should be rehighlighted.
9883             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9884             (set-buffer gnus-original-article-buffer)
9885             (gnus-request-article
9886              (cdr gnus-article-current)
9887              (car gnus-article-current) (current-buffer))))
9888         ;; Prettify the summary buffer line.
9889         (when (gnus-visual-p 'summary-highlight 'highlight)
9890           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9891
9892 (defun gnus-summary-edit-wash (key)
9893   "Perform editing command KEY in the article buffer."
9894   (interactive
9895    (list
9896     (progn
9897       (message "%s" (concat (this-command-keys) "- "))
9898       (read-char))))
9899   (message "")
9900   (gnus-summary-edit-article)
9901   (execute-kbd-macro (concat (this-command-keys) key))
9902   (gnus-article-edit-done))
9903
9904 ;;; Respooling
9905
9906 (defun gnus-summary-respool-query (&optional silent trace)
9907   "Query where the respool algorithm would put this article."
9908   (interactive)
9909   (let (gnus-mark-article-hook)
9910     (gnus-summary-select-article)
9911     (save-excursion
9912       (set-buffer gnus-original-article-buffer)
9913       (let ((groups (nnmail-article-group 'identity trace)))
9914         (unless silent
9915           (if groups
9916               (message "This message would go to %s"
9917                        (mapconcat 'car groups ", "))
9918             (message "This message would go to no groups"))
9919           groups)))))
9920
9921 (defun gnus-summary-respool-trace ()
9922   "Trace where the respool algorithm would put this article.
9923 Display a buffer showing all fancy splitting patterns which matched."
9924   (interactive)
9925   (gnus-summary-respool-query nil t))
9926
9927 ;; Summary marking commands.
9928
9929 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9930   "Mark articles which has the same subject as read, and then select the next.
9931 If UNMARK is positive, remove any kind of mark.
9932 If UNMARK is negative, tick articles."
9933   (interactive "P")
9934   (when unmark
9935     (setq unmark (prefix-numeric-value unmark)))
9936   (let ((count
9937          (gnus-summary-mark-same-subject
9938           (gnus-summary-article-subject) unmark)))
9939     ;; Select next unread article.  If auto-select-same mode, should
9940     ;; select the first unread article.
9941     (gnus-summary-next-article t (and gnus-auto-select-same
9942                                       (gnus-summary-article-subject)))
9943     (gnus-message 7 "%d article%s marked as %s"
9944                   count (if (= count 1) " is" "s are")
9945                   (if unmark "unread" "read"))))
9946
9947 (defun gnus-summary-kill-same-subject (&optional unmark)
9948   "Mark articles which has the same subject as read.
9949 If UNMARK is positive, remove any kind of mark.
9950 If UNMARK is negative, tick articles."
9951   (interactive "P")
9952   (when unmark
9953     (setq unmark (prefix-numeric-value unmark)))
9954   (let ((count
9955          (gnus-summary-mark-same-subject
9956           (gnus-summary-article-subject) unmark)))
9957     ;; If marked as read, go to next unread subject.
9958     (when (null unmark)
9959       ;; Go to next unread subject.
9960       (gnus-summary-next-subject 1 t))
9961     (gnus-message 7 "%d articles are marked as %s"
9962                   count (if unmark "unread" "read"))))
9963
9964 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9965   "Mark articles with same SUBJECT as read, and return marked number.
9966 If optional argument UNMARK is positive, remove any kinds of marks.
9967 If optional argument UNMARK is negative, mark articles as unread instead."
9968   (let ((count 1))
9969     (save-excursion
9970       (cond
9971        ((null unmark)                   ; Mark as read.
9972         (while (and
9973                 (progn
9974                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9975                   (gnus-summary-show-thread) t)
9976                 (gnus-summary-find-subject subject))
9977           (setq count (1+ count))))
9978        ((> unmark 0)                    ; Tick.
9979         (while (and
9980                 (progn
9981                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9982                   (gnus-summary-show-thread) t)
9983                 (gnus-summary-find-subject subject))
9984           (setq count (1+ count))))
9985        (t                               ; Mark as unread.
9986         (while (and
9987                 (progn
9988                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9989                   (gnus-summary-show-thread) t)
9990                 (gnus-summary-find-subject subject))
9991           (setq count (1+ count)))))
9992       (gnus-set-mode-line 'summary)
9993       ;; Return the number of marked articles.
9994       count)))
9995
9996 (defun gnus-summary-mark-as-processable (n &optional unmark)
9997   "Set the process mark on the next N articles.
9998 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9999 the process mark instead.  The difference between N and the actual
10000 number of articles marked is returned."
10001   (interactive "P")
10002   (if (and (null n) (gnus-region-active-p))
10003       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10004     (setq n (prefix-numeric-value n))
10005     (let ((backward (< n 0))
10006           (n (abs n)))
10007       (while (and
10008               (> n 0)
10009               (if unmark
10010                   (gnus-summary-remove-process-mark
10011                    (gnus-summary-article-number))
10012                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10013               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10014         (setq n (1- n)))
10015       (when (/= 0 n)
10016         (gnus-message 7 "No more articles"))
10017       (gnus-summary-recenter)
10018       (gnus-summary-position-point)
10019       n)))
10020
10021 (defun gnus-summary-unmark-as-processable (n)
10022   "Remove the process mark from the next N articles.
10023 If N is negative, unmark backward instead.  The difference between N and
10024 the actual number of articles unmarked is returned."
10025   (interactive "P")
10026   (gnus-summary-mark-as-processable n t))
10027
10028 (defun gnus-summary-unmark-all-processable ()
10029   "Remove the process mark from all articles."
10030   (interactive)
10031   (save-excursion
10032     (while gnus-newsgroup-processable
10033       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10034   (gnus-summary-position-point))
10035
10036 (defun gnus-summary-add-mark (article type)
10037   "Mark ARTICLE with a mark of TYPE."
10038   (let ((vtype (car (assq type gnus-article-mark-lists)))
10039         var)
10040     (if (not vtype)
10041         (error "No such mark type: %s" type)
10042       (setq var (intern (format "gnus-newsgroup-%s" type)))
10043       (set var (cons article (symbol-value var)))
10044       (if (memq type '(processable cached replied forwarded recent saved))
10045           (gnus-summary-update-secondary-mark article)
10046         ;;; !!! This is bogus.  We should find out what primary
10047         ;;; !!! mark we want to set.
10048         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10049
10050 (defun gnus-summary-mark-as-expirable (n)
10051   "Mark N articles forward as expirable.
10052 If N is negative, mark backward instead.  The difference between N and
10053 the actual number of articles marked is returned."
10054   (interactive "p")
10055   (gnus-summary-mark-forward n gnus-expirable-mark))
10056
10057 (defun gnus-summary-mark-as-spam (n)
10058   "Mark N articles forward as spam.
10059 If N is negative, mark backward instead.  The difference between N and
10060 the actual number of articles marked is returned."
10061   (interactive "p")
10062   (gnus-summary-mark-forward n gnus-spam-mark))
10063
10064 (defun gnus-summary-mark-article-as-replied (article)
10065   "Mark ARTICLE as replied to and update the summary line.
10066 ARTICLE can also be a list of articles."
10067   (interactive (list (gnus-summary-article-number)))
10068   (let ((articles (if (listp article) article (list article))))
10069     (dolist (article articles)
10070       (unless (numberp article)
10071         (error "%s is not a number" article))
10072       (push article gnus-newsgroup-replied)
10073       (let ((buffer-read-only nil))
10074         (when (gnus-summary-goto-subject article nil t)
10075           (gnus-summary-update-secondary-mark article))))))
10076
10077 (defun gnus-summary-mark-article-as-forwarded (article)
10078   "Mark ARTICLE as forwarded and update the summary line.
10079 ARTICLE can also be a list of articles."
10080   (let ((articles (if (listp article) article (list article))))
10081     (dolist (article articles)
10082       (push article gnus-newsgroup-forwarded)
10083       (let ((buffer-read-only nil))
10084         (when (gnus-summary-goto-subject article nil t)
10085           (gnus-summary-update-secondary-mark article))))))
10086
10087 (defun gnus-summary-set-bookmark (article)
10088   "Set a bookmark in current article."
10089   (interactive (list (gnus-summary-article-number)))
10090   (when (or (not (get-buffer gnus-article-buffer))
10091             (not gnus-current-article)
10092             (not gnus-article-current)
10093             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10094     (error "No current article selected"))
10095   ;; Remove old bookmark, if one exists.
10096   (gnus-pull article gnus-newsgroup-bookmarks)
10097   ;; Set the new bookmark, which is on the form
10098   ;; (article-number . line-number-in-body).
10099   (push
10100    (cons article
10101          (with-current-buffer gnus-article-buffer
10102            (count-lines
10103             (min (point)
10104                  (save-excursion
10105                    (article-goto-body)
10106                    (point)))
10107             (point))))
10108    gnus-newsgroup-bookmarks)
10109   (gnus-message 6 "A bookmark has been added to the current article."))
10110
10111 (defun gnus-summary-remove-bookmark (article)
10112   "Remove the bookmark from the current article."
10113   (interactive (list (gnus-summary-article-number)))
10114   ;; Remove old bookmark, if one exists.
10115   (if (not (assq article gnus-newsgroup-bookmarks))
10116       (gnus-message 6 "No bookmark in current article.")
10117     (gnus-pull article gnus-newsgroup-bookmarks)
10118     (gnus-message 6 "Removed bookmark.")))
10119
10120 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10121 (defun gnus-summary-mark-as-dormant (n)
10122   "Mark N articles forward as dormant.
10123 If N is negative, mark backward instead.  The difference between N and
10124 the actual number of articles marked is returned."
10125   (interactive "p")
10126   (gnus-summary-mark-forward n gnus-dormant-mark))
10127
10128 (defun gnus-summary-set-process-mark (article)
10129   "Set the process mark on ARTICLE and update the summary line."
10130   (setq gnus-newsgroup-processable
10131         (cons article
10132               (delq article gnus-newsgroup-processable)))
10133   (when (gnus-summary-goto-subject article)
10134     (gnus-summary-show-thread)
10135     (gnus-summary-goto-subject article)
10136     (gnus-summary-update-secondary-mark article)))
10137
10138 (defun gnus-summary-remove-process-mark (&rest articles)
10139   "Remove the process mark from ARTICLES and update the summary line."
10140   (dolist (article articles)
10141     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10142     (when (gnus-summary-goto-subject article)
10143       (gnus-summary-show-thread)
10144       (gnus-summary-goto-subject article)
10145       (gnus-summary-update-secondary-mark article)))
10146   t)
10147
10148 (defun gnus-summary-set-saved-mark (article)
10149   "Set the process mark on ARTICLE and update the summary line."
10150   (push article gnus-newsgroup-saved)
10151   (when (gnus-summary-goto-subject article)
10152     (gnus-summary-update-secondary-mark article)))
10153
10154 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10155   "Mark N articles as read forwards.
10156 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10157 The difference between N and the actual number of articles marked is
10158 returned.
10159 If NO-EXPIRE, auto-expiry will be inhibited."
10160   (interactive "p")
10161   (gnus-summary-show-thread)
10162   (let ((backward (< n 0))
10163         (gnus-summary-goto-unread
10164          (and gnus-summary-goto-unread
10165               (not (eq gnus-summary-goto-unread 'never))
10166               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10167                                     gnus-ticked-mark gnus-dormant-mark)))))
10168         (n (abs n))
10169         (mark (or mark gnus-del-mark)))
10170     (while (and (> n 0)
10171                 (gnus-summary-mark-article nil mark no-expire)
10172                 (zerop (gnus-summary-next-subject
10173                         (if backward -1 1)
10174                         (and gnus-summary-goto-unread
10175                              (not (eq gnus-summary-goto-unread 'never)))
10176                         t)))
10177       (setq n (1- n)))
10178     (when (/= 0 n)
10179       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10180     (gnus-summary-recenter)
10181     (gnus-summary-position-point)
10182     (gnus-set-mode-line 'summary)
10183     n))
10184
10185 (defun gnus-summary-mark-article-as-read (mark)
10186   "Mark the current article quickly as read with MARK."
10187   (let ((article (gnus-summary-article-number)))
10188     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10189     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10190     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10191     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10192     (push (cons article mark) gnus-newsgroup-reads)
10193     ;; Possibly remove from cache, if that is used.
10194     (when gnus-use-cache
10195       (gnus-cache-enter-remove-article article))
10196     ;; Allow the backend to change the mark.
10197     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10198     ;; Check for auto-expiry.
10199     (when (and gnus-newsgroup-auto-expire
10200                (memq mark gnus-auto-expirable-marks))
10201       (setq mark gnus-expirable-mark)
10202       ;; Let the backend know about the mark change.
10203       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10204       (push article gnus-newsgroup-expirable))
10205     ;; Set the mark in the buffer.
10206     (gnus-summary-update-mark mark 'unread)
10207     t))
10208
10209 (defun gnus-summary-mark-article-as-unread (mark)
10210   "Mark the current article quickly as unread with MARK."
10211   (let* ((article (gnus-summary-article-number))
10212          (old-mark (gnus-summary-article-mark article)))
10213     ;; Allow the backend to change the mark.
10214     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10215     (if (eq mark old-mark)
10216         t
10217       (if (<= article 0)
10218           (progn
10219             (gnus-error 1 "Can't mark negative article numbers")
10220             nil)
10221         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10222         (setq gnus-newsgroup-spam-marked
10223               (delq article gnus-newsgroup-spam-marked))
10224         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10225         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10226         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10227         (cond ((= mark gnus-ticked-mark)
10228                (setq gnus-newsgroup-marked
10229                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10230                                               article)))
10231               ((= mark gnus-spam-mark)
10232                (setq gnus-newsgroup-spam-marked
10233                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10234                                               article)))
10235               ((= mark gnus-dormant-mark)
10236                (setq gnus-newsgroup-dormant
10237                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10238                                               article)))
10239               (t
10240                (setq gnus-newsgroup-unreads
10241                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10242                                               article))))
10243         (gnus-pull article gnus-newsgroup-reads)
10244
10245         ;; See whether the article is to be put in the cache.
10246         (and gnus-use-cache
10247              (vectorp (gnus-summary-article-header article))
10248              (save-excursion
10249                (gnus-cache-possibly-enter-article
10250                 gnus-newsgroup-name article
10251                 (= mark gnus-ticked-mark)
10252                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10253
10254         ;; Fix the mark.
10255         (gnus-summary-update-mark mark 'unread)
10256         t))))
10257
10258 (defun gnus-summary-mark-article (&optional article mark no-expire)
10259   "Mark ARTICLE with MARK.  MARK can be any character.
10260 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10261 `??' (dormant) and `?E' (expirable).
10262 If MARK is nil, then the default character `?r' is used.
10263 If ARTICLE is nil, then the article on the current line will be
10264 marked.
10265 If NO-EXPIRE, auto-expiry will be inhibited."
10266   ;; The mark might be a string.
10267   (when (stringp mark)
10268     (setq mark (aref mark 0)))
10269   ;; If no mark is given, then we check auto-expiring.
10270   (when (null mark)
10271     (setq mark gnus-del-mark))
10272   (when (and (not no-expire)
10273              gnus-newsgroup-auto-expire
10274              (memq mark gnus-auto-expirable-marks))
10275     (setq mark gnus-expirable-mark))
10276   (let ((article (or 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       (unless article
10283         (error "No article on current line"))
10284       (if (not (if (or (= mark gnus-unread-mark)
10285                        (= mark gnus-ticked-mark)
10286                        (= mark gnus-spam-mark)
10287                        (= mark gnus-dormant-mark))
10288                    (gnus-mark-article-as-unread article mark)
10289                  (gnus-mark-article-as-read article mark)))
10290           t
10291         ;; See whether the article is to be put in the cache.
10292         (and gnus-use-cache
10293              (not (= mark gnus-canceled-mark))
10294              (vectorp (gnus-summary-article-header article))
10295              (save-excursion
10296                (gnus-cache-possibly-enter-article
10297                 gnus-newsgroup-name article
10298                 (= mark gnus-ticked-mark)
10299                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10300
10301         (when (gnus-summary-goto-subject article nil t)
10302           (let ((buffer-read-only nil))
10303             (gnus-summary-show-thread)
10304             ;; Fix the mark.
10305             (gnus-summary-update-mark mark 'unread)
10306             t))))))
10307
10308 (defun gnus-summary-update-secondary-mark (article)
10309   "Update the secondary (read, process, cache) mark."
10310   (gnus-summary-update-mark
10311    (cond ((memq article gnus-newsgroup-processable)
10312           gnus-process-mark)
10313          ((memq article gnus-newsgroup-cached)
10314           gnus-cached-mark)
10315          ((memq article gnus-newsgroup-replied)
10316           gnus-replied-mark)
10317          ((memq article gnus-newsgroup-forwarded)
10318           gnus-forwarded-mark)
10319          ((memq article gnus-newsgroup-saved)
10320           gnus-saved-mark)
10321          ((memq article gnus-newsgroup-recent)
10322           gnus-recent-mark)
10323          ((memq article gnus-newsgroup-unseen)
10324           gnus-unseen-mark)
10325          (t gnus-no-mark))
10326    'replied)
10327   (when (gnus-visual-p 'summary-highlight 'highlight)
10328     (gnus-run-hooks 'gnus-summary-update-hook))
10329   t)
10330
10331 (defun gnus-summary-update-download-mark (article)
10332   "Update the download mark."
10333   (gnus-summary-update-mark
10334    (cond ((memq article gnus-newsgroup-undownloaded)
10335           gnus-undownloaded-mark)
10336          (gnus-newsgroup-agentized
10337           gnus-downloaded-mark)
10338          (t
10339           gnus-no-mark))
10340    'download)
10341   (gnus-summary-update-line t)
10342   t)
10343
10344 (defun gnus-summary-update-mark (mark type)
10345   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10346         (buffer-read-only nil))
10347     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10348     (when forward
10349       (when (looking-at "\r")
10350         (incf forward))
10351       (when (<= (+ forward (point)) (point-max))
10352         ;; Go to the right position on the line.
10353         (goto-char (+ forward (point)))
10354         ;; Replace the old mark with the new mark.
10355         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10356         ;; Optionally update the marks by some user rule.
10357         (when (eq type 'unread)
10358           (gnus-data-set-mark
10359            (gnus-data-find (gnus-summary-article-number)) mark)
10360           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10361
10362 (defun gnus-mark-article-as-read (article &optional mark)
10363   "Enter ARTICLE in the pertinent lists and remove it from others."
10364   ;; Make the article expirable.
10365   (let ((mark (or mark gnus-del-mark)))
10366     (setq gnus-newsgroup-expirable
10367           (if (= mark gnus-expirable-mark)
10368               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10369             (delq article gnus-newsgroup-expirable)))
10370     ;; Remove from unread and marked lists.
10371     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10372     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10373     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10374     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10375     (push (cons article mark) gnus-newsgroup-reads)
10376     ;; Possibly remove from cache, if that is used.
10377     (when gnus-use-cache
10378       (gnus-cache-enter-remove-article article))
10379     t))
10380
10381 (defun gnus-mark-article-as-unread (article &optional mark)
10382   "Enter ARTICLE in the pertinent lists and remove it from others."
10383   (let ((mark (or mark gnus-ticked-mark)))
10384     (if (<= article 0)
10385         (progn
10386           (gnus-error 1 "Can't mark negative article numbers")
10387           nil)
10388       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10389             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10390             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10391             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10392             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10393
10394       ;; Unsuppress duplicates?
10395       (when gnus-suppress-duplicates
10396         (gnus-dup-unsuppress-article article))
10397
10398       (cond ((= mark gnus-ticked-mark)
10399              (setq gnus-newsgroup-marked
10400                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10401             ((= mark gnus-spam-mark)
10402              (setq gnus-newsgroup-spam-marked
10403                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10404                                             article)))
10405             ((= mark gnus-dormant-mark)
10406              (setq gnus-newsgroup-dormant
10407                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10408             (t
10409              (setq gnus-newsgroup-unreads
10410                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10411       (gnus-pull article gnus-newsgroup-reads)
10412       t)))
10413
10414 (defalias 'gnus-summary-mark-as-unread-forward
10415   'gnus-summary-tick-article-forward)
10416 (make-obsolete 'gnus-summary-mark-as-unread-forward
10417                'gnus-summary-tick-article-forward)
10418 (defun gnus-summary-tick-article-forward (n)
10419   "Tick N articles forwards.
10420 If N is negative, tick backwards instead.
10421 The difference between N and the number of articles ticked is returned."
10422   (interactive "p")
10423   (gnus-summary-mark-forward n gnus-ticked-mark))
10424
10425 (defalias 'gnus-summary-mark-as-unread-backward
10426   'gnus-summary-tick-article-backward)
10427 (make-obsolete 'gnus-summary-mark-as-unread-backward
10428                'gnus-summary-tick-article-backward)
10429 (defun gnus-summary-tick-article-backward (n)
10430   "Tick N articles backwards.
10431 The difference between N and the number of articles ticked is returned."
10432   (interactive "p")
10433   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10434
10435 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10436 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10437 (defun gnus-summary-tick-article (&optional article clear-mark)
10438   "Mark current article as unread.
10439 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10440 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10441   (interactive)
10442   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10443                                        gnus-ticked-mark)))
10444
10445 (defun gnus-summary-mark-as-read-forward (n)
10446   "Mark N articles as read forwards.
10447 If N is negative, mark backwards instead.
10448 The difference between N and the actual number of articles marked is
10449 returned."
10450   (interactive "p")
10451   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10452
10453 (defun gnus-summary-mark-as-read-backward (n)
10454   "Mark the N articles as read backwards.
10455 The difference between N and the actual number of articles marked is
10456 returned."
10457   (interactive "p")
10458   (gnus-summary-mark-forward
10459    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10460
10461 (defun gnus-summary-mark-as-read (&optional article mark)
10462   "Mark current article as read.
10463 ARTICLE specifies the article to be marked as read.
10464 MARK specifies a string to be inserted at the beginning of the line."
10465   (gnus-summary-mark-article article mark))
10466
10467 (defun gnus-summary-clear-mark-forward (n)
10468   "Clear marks from N articles forward.
10469 If N is negative, clear backward instead.
10470 The difference between N and the number of marks cleared is returned."
10471   (interactive "p")
10472   (gnus-summary-mark-forward n gnus-unread-mark))
10473
10474 (defun gnus-summary-clear-mark-backward (n)
10475   "Clear marks from N articles backward.
10476 The difference between N and the number of marks cleared is returned."
10477   (interactive "p")
10478   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10479
10480 (defun gnus-summary-mark-unread-as-read ()
10481   "Intended to be used by `gnus-summary-mark-article-hook'."
10482   (when (memq gnus-current-article gnus-newsgroup-unreads)
10483     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10484
10485 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10486   "Intended to be used by `gnus-summary-mark-article-hook'."
10487   (let ((mark (gnus-summary-article-mark)))
10488     (when (or (gnus-unread-mark-p mark)
10489               (gnus-read-mark-p mark))
10490       (gnus-summary-mark-article gnus-current-article
10491                                  (or new-mark gnus-read-mark)))))
10492
10493 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10494   "Intended to be used by `gnus-summary-mark-article-hook'."
10495   (let ((mark (gnus-summary-article-mark)))
10496     (when (or (gnus-unread-mark-p mark)
10497               (gnus-read-mark-p mark))
10498       (gnus-summary-mark-article (gnus-summary-article-number)
10499                                  (or new-mark gnus-read-mark)))))
10500
10501 (defun gnus-summary-mark-unread-as-ticked ()
10502   "Intended to be used by `gnus-summary-mark-article-hook'."
10503   (when (memq gnus-current-article gnus-newsgroup-unreads)
10504     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10505
10506 (defun gnus-summary-mark-region-as-read (point mark all)
10507   "Mark all unread articles between point and mark as read.
10508 If given a prefix, mark all articles between point and mark as read,
10509 even ticked and dormant ones."
10510   (interactive "r\nP")
10511   (save-excursion
10512     (let (article)
10513       (goto-char point)
10514       (beginning-of-line)
10515       (while (and
10516               (< (point) mark)
10517               (progn
10518                 (when (or all
10519                           (memq (setq article (gnus-summary-article-number))
10520                                 gnus-newsgroup-unreads))
10521                   (gnus-summary-mark-article article gnus-del-mark))
10522                 t)
10523               (gnus-summary-find-next))))))
10524
10525 (defun gnus-summary-mark-below (score mark)
10526   "Mark articles with score less than SCORE with MARK."
10527   (interactive "P\ncMark: ")
10528   (setq score (if score
10529                   (prefix-numeric-value score)
10530                 (or gnus-summary-default-score 0)))
10531   (save-excursion
10532     (set-buffer gnus-summary-buffer)
10533     (goto-char (point-min))
10534     (while
10535         (progn
10536           (and (< (gnus-summary-article-score) score)
10537                (gnus-summary-mark-article nil mark))
10538           (gnus-summary-find-next)))))
10539
10540 (defun gnus-summary-kill-below (&optional score)
10541   "Mark articles with score below SCORE as read."
10542   (interactive "P")
10543   (gnus-summary-mark-below score gnus-killed-mark))
10544
10545 (defun gnus-summary-clear-above (&optional score)
10546   "Clear all marks from articles with score above SCORE."
10547   (interactive "P")
10548   (gnus-summary-mark-above score gnus-unread-mark))
10549
10550 (defun gnus-summary-tick-above (&optional score)
10551   "Tick all articles with score above SCORE."
10552   (interactive "P")
10553   (gnus-summary-mark-above score gnus-ticked-mark))
10554
10555 (defun gnus-summary-mark-above (score mark)
10556   "Mark articles with score over SCORE with MARK."
10557   (interactive "P\ncMark: ")
10558   (setq score (if score
10559                   (prefix-numeric-value score)
10560                 (or gnus-summary-default-score 0)))
10561   (save-excursion
10562     (set-buffer gnus-summary-buffer)
10563     (goto-char (point-min))
10564     (while (and (progn
10565                   (when (> (gnus-summary-article-score) score)
10566                     (gnus-summary-mark-article nil mark))
10567                   t)
10568                 (gnus-summary-find-next)))))
10569
10570 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10571 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10572 (defun gnus-summary-limit-include-expunged (&optional no-error)
10573   "Display all the hidden articles that were expunged for low scores."
10574   (interactive)
10575   (let ((buffer-read-only nil))
10576     (let ((scored gnus-newsgroup-scored)
10577           headers h)
10578       (while scored
10579         (unless (gnus-summary-article-header (caar scored))
10580           (and (setq h (gnus-number-to-header (caar scored)))
10581                (< (cdar scored) gnus-summary-expunge-below)
10582                (push h headers)))
10583         (setq scored (cdr scored)))
10584       (if (not headers)
10585           (when (not no-error)
10586             (error "No expunged articles hidden"))
10587         (goto-char (point-min))
10588         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10589         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10590         (mapcar (lambda (x) (push (mail-header-number x)
10591                                   gnus-newsgroup-limit))
10592                 headers)
10593         (gnus-summary-prepare-unthreaded (nreverse headers))
10594         (goto-char (point-min))
10595         (gnus-summary-position-point)
10596         t))))
10597
10598 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10599   "Mark all unread articles in this newsgroup as read.
10600 If prefix argument ALL is non-nil, ticked and dormant articles will
10601 also be marked as read.
10602 If QUIETLY is non-nil, no questions will be asked.
10603
10604 If TO-HERE is non-nil, it should be a point in the buffer.  All
10605 articles before (after, if REVERSE is set) this point will be marked
10606 as read.
10607
10608 Note that this function will only catch up the unread article
10609 in the current summary buffer limitation.
10610
10611 The number of articles marked as read is returned."
10612   (interactive "P")
10613   (prog1
10614       (save-excursion
10615         (when (or quietly
10616                   (not gnus-interactive-catchup) ;Without confirmation?
10617                   gnus-expert-user
10618                   (gnus-y-or-n-p
10619                    (if all
10620                        "Mark absolutely all articles as read? "
10621                      "Mark all unread articles as read? ")))
10622           (if (and not-mark
10623                    (not gnus-newsgroup-adaptive)
10624                    (not gnus-newsgroup-auto-expire)
10625                    (not gnus-suppress-duplicates)
10626                    (or (not gnus-use-cache)
10627                        (eq gnus-use-cache 'passive)))
10628               (progn
10629                 (when all
10630                   (setq gnus-newsgroup-marked nil
10631                         gnus-newsgroup-spam-marked nil
10632                         gnus-newsgroup-dormant nil))
10633                 (setq gnus-newsgroup-unreads
10634                       (gnus-sorted-nunion
10635                        (gnus-intersection gnus-newsgroup-unreads
10636                                           gnus-newsgroup-downloadable)
10637                        gnus-newsgroup-unfetched)))
10638             ;; We actually mark all articles as canceled, which we
10639             ;; have to do when using auto-expiry or adaptive scoring.
10640             (gnus-summary-show-all-threads)
10641             (if (and to-here reverse)
10642                 (progn
10643                   (goto-char to-here)
10644                   (gnus-summary-mark-current-read-and-unread-as-read
10645                    gnus-catchup-mark)
10646                   (while (gnus-summary-find-next (not all))
10647                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10648               (when (gnus-summary-first-subject (not all))
10649                 (while (and
10650                         (if to-here (< (point) to-here) t)
10651                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10652                         (gnus-summary-find-next (not all))))))
10653             (gnus-set-mode-line 'summary))
10654           t))
10655     (gnus-summary-position-point)))
10656
10657 (defun gnus-summary-catchup-to-here (&optional all)
10658   "Mark all unticked articles before the current one as read.
10659 If ALL is non-nil, also mark ticked and dormant articles as read."
10660   (interactive "P")
10661   (save-excursion
10662     (gnus-save-hidden-threads
10663       (let ((beg (point)))
10664         ;; We check that there are unread articles.
10665         (when (or all (gnus-summary-find-prev))
10666           (gnus-summary-catchup all t beg)))))
10667   (gnus-summary-position-point))
10668
10669 (defun gnus-summary-catchup-from-here (&optional all)
10670   "Mark all unticked articles after (and including) the current one as read.
10671 If ALL is non-nil, also mark ticked and dormant articles as read."
10672   (interactive "P")
10673   (save-excursion
10674     (gnus-save-hidden-threads
10675       (let ((beg (point)))
10676         ;; We check that there are unread articles.
10677         (when (or all (gnus-summary-find-next))
10678           (gnus-summary-catchup all t beg nil t)))))
10679   (gnus-summary-position-point))
10680
10681 (defun gnus-summary-catchup-all (&optional quietly)
10682   "Mark all articles in this newsgroup as read.
10683 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10684 instead, which marks only unread articles as read."
10685   (interactive "P")
10686   (gnus-summary-catchup t quietly))
10687
10688 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10689   "Mark all unread articles in this group as read, then exit.
10690 If prefix argument ALL is non-nil, all articles are marked as read.
10691 If QUIETLY is non-nil, no questions will be asked."
10692   (interactive "P")
10693   (when (gnus-summary-catchup all quietly nil 'fast)
10694     ;; Select next newsgroup or exit.
10695     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10696              (eq gnus-auto-select-next 'quietly))
10697         (gnus-summary-next-group nil)
10698       (gnus-summary-exit))))
10699
10700 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10701   "Mark all articles in this newsgroup as read, and then exit.
10702 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10703 instead, which marks only unread articles as read."
10704   (interactive "P")
10705   (gnus-summary-catchup-and-exit t quietly))
10706
10707 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10708   "Mark all articles in this group as read and select the next group.
10709 If given a prefix, mark all articles, unread as well as ticked, as
10710 read."
10711   (interactive "P")
10712   (save-excursion
10713     (gnus-summary-catchup all))
10714   (gnus-summary-next-group))
10715
10716 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
10717   "Mark all articles in this group as read and select the previous group.
10718 If given a prefix, mark all articles, unread as well as ticked, as
10719 read."
10720   (interactive "P")
10721   (save-excursion
10722     (gnus-summary-catchup all))
10723   (gnus-summary-next-group nil nil t))
10724
10725 ;;;
10726 ;;; with article
10727 ;;;
10728
10729 (defmacro gnus-with-article (article &rest forms)
10730   "Select ARTICLE and perform FORMS in the original article buffer.
10731 Then replace the article with the result."
10732   `(progn
10733      ;; We don't want the article to be marked as read.
10734      (let (gnus-mark-article-hook)
10735        (gnus-summary-select-article t t nil ,article))
10736      (set-buffer gnus-original-article-buffer)
10737      ,@forms
10738      (if (not (gnus-check-backend-function
10739                'request-replace-article (car gnus-article-current)))
10740          (gnus-message 5 "Read-only group; not replacing")
10741        (unless (gnus-request-replace-article
10742                 ,article (car gnus-article-current)
10743                 (current-buffer) t)
10744          (error "Couldn't replace article")))
10745      ;; The cache and backlog have to be flushed somewhat.
10746      (when gnus-keep-backlog
10747        (gnus-backlog-remove-article
10748         (car gnus-article-current) (cdr gnus-article-current)))
10749      (when gnus-use-cache
10750        (gnus-cache-update-article
10751         (car gnus-article-current) (cdr gnus-article-current)))))
10752
10753 (put 'gnus-with-article 'lisp-indent-function 1)
10754 (put 'gnus-with-article 'edebug-form-spec '(form body))
10755
10756 ;; Thread-based commands.
10757
10758 (defun gnus-summary-articles-in-thread (&optional article)
10759   "Return a list of all articles in the current thread.
10760 If ARTICLE is non-nil, return all articles in the thread that starts
10761 with that article."
10762   (let* ((article (or article (gnus-summary-article-number)))
10763          (data (gnus-data-find-list article))
10764          (top-level (gnus-data-level (car data)))
10765          (top-subject
10766           (cond ((null gnus-thread-operation-ignore-subject)
10767                  (gnus-simplify-subject-re
10768                   (mail-header-subject (gnus-data-header (car data)))))
10769                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10770                  (gnus-simplify-subject-fuzzy
10771                   (mail-header-subject (gnus-data-header (car data)))))
10772                 (t nil)))
10773          (end-point (save-excursion
10774                       (if (gnus-summary-go-to-next-thread)
10775                           (point) (point-max))))
10776          articles)
10777     (while (and data
10778                 (< (gnus-data-pos (car data)) end-point))
10779       (when (or (not top-subject)
10780                 (string= top-subject
10781                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10782                              (gnus-simplify-subject-fuzzy
10783                               (mail-header-subject
10784                                (gnus-data-header (car data))))
10785                            (gnus-simplify-subject-re
10786                             (mail-header-subject
10787                              (gnus-data-header (car data)))))))
10788         (push (gnus-data-number (car data)) articles))
10789       (unless (and (setq data (cdr data))
10790                    (> (gnus-data-level (car data)) top-level))
10791         (setq data nil)))
10792     ;; Return the list of articles.
10793     (nreverse articles)))
10794
10795 (defun gnus-summary-rethread-current ()
10796   "Rethread the thread the current article is part of."
10797   (interactive)
10798   (let* ((gnus-show-threads t)
10799          (article (gnus-summary-article-number))
10800          (id (mail-header-id (gnus-summary-article-header)))
10801          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10802     (unless id
10803       (error "No article on the current line"))
10804     (gnus-rebuild-thread id)
10805     (gnus-summary-goto-subject article)))
10806
10807 (defun gnus-summary-reparent-thread ()
10808   "Make the current article child of the marked (or previous) article.
10809
10810 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10811 is non-nil or the Subject: of both articles are the same."
10812   (interactive)
10813   (unless (not (gnus-group-read-only-p))
10814     (error "The current newsgroup does not support article editing"))
10815   (unless (<= (length gnus-newsgroup-processable) 1)
10816     (error "No more than one article may be marked"))
10817   (save-window-excursion
10818     (let ((gnus-article-buffer " *reparent*")
10819           (current-article (gnus-summary-article-number))
10820           ;; First grab the marked article, otherwise one line up.
10821           (parent-article (if (not (null gnus-newsgroup-processable))
10822                               (car gnus-newsgroup-processable)
10823                             (save-excursion
10824                               (if (eq (forward-line -1) 0)
10825                                   (gnus-summary-article-number)
10826                                 (error "Beginning of summary buffer"))))))
10827       (unless (not (eq current-article parent-article))
10828         (error "An article may not be self-referential"))
10829       (let ((message-id (mail-header-id
10830                          (gnus-summary-article-header parent-article))))
10831         (unless (and message-id (not (equal message-id "")))
10832           (error "No message-id in desired parent"))
10833         (gnus-with-article current-article
10834           (save-restriction
10835             (goto-char (point-min))
10836             (message-narrow-to-head)
10837             (if (re-search-forward "^References: " nil t)
10838                 (progn
10839                   (re-search-forward "^[^ \t]" nil t)
10840                   (forward-line -1)
10841                   (end-of-line)
10842                   (insert " " message-id))
10843               (insert "References: " message-id "\n"))))
10844         (set-buffer gnus-summary-buffer)
10845         (gnus-summary-unmark-all-processable)
10846         (gnus-summary-update-article current-article)
10847         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10848             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10849         (gnus-summary-rethread-current)
10850         (gnus-message 3 "Article %d is now the child of article %d"
10851                       current-article parent-article)))))
10852
10853 (defun gnus-summary-toggle-threads (&optional arg)
10854   "Toggle showing conversation threads.
10855 If ARG is positive number, turn showing conversation threads on."
10856   (interactive "P")
10857   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10858     (setq gnus-show-threads
10859           (if (null arg) (not gnus-show-threads)
10860             (> (prefix-numeric-value arg) 0)))
10861     (gnus-summary-prepare)
10862     (gnus-summary-goto-subject current)
10863     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10864     (gnus-summary-position-point)))
10865
10866 (defun gnus-summary-show-all-threads ()
10867   "Show all threads."
10868   (interactive)
10869   (save-excursion
10870     (let ((buffer-read-only nil))
10871       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10872   (gnus-summary-position-point))
10873
10874 (defun gnus-summary-show-thread ()
10875   "Show thread subtrees.
10876 Returns nil if no thread was there to be shown."
10877   (interactive)
10878   (let ((buffer-read-only nil)
10879         (orig (point))
10880         (end (point-at-eol))
10881         ;; Leave point at bol
10882         (beg (progn (beginning-of-line) (point))))
10883     (prog1
10884         ;; Any hidden lines here?
10885         (search-forward "\r" end t)
10886       (subst-char-in-region beg end ?\^M ?\n t)
10887       (goto-char orig)
10888       (gnus-summary-position-point))))
10889
10890 (defun gnus-summary-maybe-hide-threads ()
10891   "If requested, hide the threads that should be hidden."
10892   (when (and gnus-show-threads
10893              gnus-thread-hide-subtree)
10894     (gnus-summary-hide-all-threads
10895      (if (or (consp gnus-thread-hide-subtree)
10896              (functionp gnus-thread-hide-subtree))
10897          (gnus-make-predicate gnus-thread-hide-subtree)
10898        nil))))
10899
10900 ;;; Hiding predicates.
10901
10902 (defun gnus-article-unread-p (header)
10903   (memq (mail-header-number header) gnus-newsgroup-unreads))
10904
10905 (defun gnus-article-unseen-p (header)
10906   (memq (mail-header-number header) gnus-newsgroup-unseen))
10907
10908 (defun gnus-map-articles (predicate articles)
10909   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10910   (apply 'gnus-or (mapcar predicate
10911                           (mapcar 'gnus-summary-article-header articles))))
10912
10913 (defun gnus-summary-hide-all-threads (&optional predicate)
10914   "Hide all thread subtrees.
10915 If PREDICATE is supplied, threads that satisfy this predicate
10916 will not be hidden."
10917   (interactive)
10918   (save-excursion
10919     (goto-char (point-min))
10920     (let ((end nil))
10921       (while (not end)
10922         (when (or (not predicate)
10923                   (gnus-map-articles
10924                    predicate (gnus-summary-article-children)))
10925             (gnus-summary-hide-thread))
10926         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10927   (gnus-summary-position-point))
10928
10929 (defun gnus-summary-hide-thread ()
10930   "Hide thread subtrees.
10931 If PREDICATE is supplied, threads that satisfy this predicate
10932 will not be hidden.
10933 Returns nil if no threads were there to be hidden."
10934   (interactive)
10935   (let ((buffer-read-only nil)
10936         (start (point))
10937         (article (gnus-summary-article-number)))
10938     (goto-char start)
10939     ;; Go forward until either the buffer ends or the subthread
10940     ;; ends.
10941     (when (and (not (eobp))
10942                (or (zerop (gnus-summary-next-thread 1 t))
10943                    (goto-char (point-max))))
10944       (prog1
10945           (if (and (> (point) start)
10946                    (search-backward "\n" start t))
10947               (progn
10948                 (subst-char-in-region start (point) ?\n ?\^M)
10949                 (gnus-summary-goto-subject article))
10950             (goto-char start)
10951             nil)))))
10952
10953 (defun gnus-summary-go-to-next-thread (&optional previous)
10954   "Go to the same level (or less) next thread.
10955 If PREVIOUS is non-nil, go to previous thread instead.
10956 Return the article number moved to, or nil if moving was impossible."
10957   (let ((level (gnus-summary-thread-level))
10958         (way (if previous -1 1))
10959         (beg (point)))
10960     (forward-line way)
10961     (while (and (not (eobp))
10962                 (< level (gnus-summary-thread-level)))
10963       (forward-line way))
10964     (if (eobp)
10965         (progn
10966           (goto-char beg)
10967           nil)
10968       (setq beg (point))
10969       (prog1
10970           (gnus-summary-article-number)
10971         (goto-char beg)))))
10972
10973 (defun gnus-summary-next-thread (n &optional silent)
10974   "Go to the same level next N'th thread.
10975 If N is negative, search backward instead.
10976 Returns the difference between N and the number of skips actually
10977 done.
10978
10979 If SILENT, don't output messages."
10980   (interactive "p")
10981   (let ((backward (< n 0))
10982         (n (abs n)))
10983     (while (and (> n 0)
10984                 (gnus-summary-go-to-next-thread backward))
10985       (decf n))
10986     (unless silent
10987       (gnus-summary-position-point))
10988     (when (and (not silent) (/= 0 n))
10989       (gnus-message 7 "No more threads"))
10990     n))
10991
10992 (defun gnus-summary-prev-thread (n)
10993   "Go to the same level previous N'th thread.
10994 Returns the difference between N and the number of skips actually
10995 done."
10996   (interactive "p")
10997   (gnus-summary-next-thread (- n)))
10998
10999 (defun gnus-summary-go-down-thread ()
11000   "Go down one level in the current thread."
11001   (let ((children (gnus-summary-article-children)))
11002     (when children
11003       (gnus-summary-goto-subject (car children)))))
11004
11005 (defun gnus-summary-go-up-thread ()
11006   "Go up one level in the current thread."
11007   (let ((parent (gnus-summary-article-parent)))
11008     (when parent
11009       (gnus-summary-goto-subject parent))))
11010
11011 (defun gnus-summary-down-thread (n)
11012   "Go down thread N steps.
11013 If N is negative, go up instead.
11014 Returns the difference between N and how many steps down that were
11015 taken."
11016   (interactive "p")
11017   (let ((up (< n 0))
11018         (n (abs n)))
11019     (while (and (> n 0)
11020                 (if up (gnus-summary-go-up-thread)
11021                   (gnus-summary-go-down-thread)))
11022       (setq n (1- n)))
11023     (gnus-summary-position-point)
11024     (when (/= 0 n)
11025       (gnus-message 7 "Can't go further"))
11026     n))
11027
11028 (defun gnus-summary-up-thread (n)
11029   "Go up thread N steps.
11030 If N is negative, go down instead.
11031 Returns the difference between N and how many steps down that were
11032 taken."
11033   (interactive "p")
11034   (gnus-summary-down-thread (- n)))
11035
11036 (defun gnus-summary-top-thread ()
11037   "Go to the top of the thread."
11038   (interactive)
11039   (while (gnus-summary-go-up-thread))
11040   (gnus-summary-article-number))
11041
11042 (defun gnus-summary-kill-thread (&optional unmark)
11043   "Mark articles under current thread as read.
11044 If the prefix argument is positive, remove any kinds of marks.
11045 If the prefix argument is negative, tick articles instead."
11046   (interactive "P")
11047   (when unmark
11048     (setq unmark (prefix-numeric-value unmark)))
11049   (let ((articles (gnus-summary-articles-in-thread)))
11050     (save-excursion
11051       ;; Expand the thread.
11052       (gnus-summary-show-thread)
11053       ;; Mark all the articles.
11054       (while articles
11055         (gnus-summary-goto-subject (car articles))
11056         (cond ((null unmark)
11057                (gnus-summary-mark-article-as-read gnus-killed-mark))
11058               ((> unmark 0)
11059                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11060               (t
11061                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11062         (setq articles (cdr articles))))
11063     ;; Hide killed subtrees.
11064     (and (null unmark)
11065          gnus-thread-hide-killed
11066          (gnus-summary-hide-thread))
11067     ;; If marked as read, go to next unread subject.
11068     (when (null unmark)
11069       ;; Go to next unread subject.
11070       (gnus-summary-next-subject 1 t)))
11071   (gnus-set-mode-line 'summary))
11072
11073 ;; Summary sorting commands
11074
11075 (defun gnus-summary-sort-by-number (&optional reverse)
11076   "Sort the summary buffer by article number.
11077 Argument REVERSE means reverse order."
11078   (interactive "P")
11079   (gnus-summary-sort 'number reverse))
11080
11081 (defun gnus-summary-sort-by-random (&optional reverse)
11082   "Randomize the order in the summary buffer.
11083 Argument REVERSE means to randomize in reverse order."
11084   (interactive "P")
11085   (gnus-summary-sort 'random reverse))
11086
11087 (defun gnus-summary-sort-by-author (&optional reverse)
11088   "Sort the summary buffer by author name alphabetically.
11089 If `case-fold-search' is non-nil, case of letters is ignored.
11090 Argument REVERSE means reverse order."
11091   (interactive "P")
11092   (gnus-summary-sort 'author reverse))
11093
11094 (defun gnus-summary-sort-by-recipient (&optional reverse)
11095   "Sort the summary buffer by recipient name alphabetically.
11096 If `case-fold-search' is non-nil, case of letters is ignored.
11097 Argument REVERSE means reverse order."
11098   (interactive "P")
11099   (gnus-summary-sort 'recipient reverse))
11100
11101 (defun gnus-summary-sort-by-subject (&optional reverse)
11102   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11103 If `case-fold-search' is non-nil, case of letters is ignored.
11104 Argument REVERSE means reverse order."
11105   (interactive "P")
11106   (gnus-summary-sort 'subject reverse))
11107
11108 (defun gnus-summary-sort-by-date (&optional reverse)
11109   "Sort the summary buffer by date.
11110 Argument REVERSE means reverse order."
11111   (interactive "P")
11112   (gnus-summary-sort 'date reverse))
11113
11114 (defun gnus-summary-sort-by-score (&optional reverse)
11115   "Sort the summary buffer by score.
11116 Argument REVERSE means reverse order."
11117   (interactive "P")
11118   (gnus-summary-sort 'score reverse))
11119
11120 (defun gnus-summary-sort-by-lines (&optional reverse)
11121   "Sort the summary buffer by the number of lines.
11122 Argument REVERSE means reverse order."
11123   (interactive "P")
11124   (gnus-summary-sort 'lines reverse))
11125
11126 (defun gnus-summary-sort-by-chars (&optional reverse)
11127   "Sort the summary buffer by article length.
11128 Argument REVERSE means reverse order."
11129   (interactive "P")
11130   (gnus-summary-sort 'chars reverse))
11131
11132 (defun gnus-summary-sort-by-original (&optional reverse)
11133   "Sort the summary buffer using the default sorting method.
11134 Argument REVERSE means reverse order."
11135   (interactive "P")
11136   (let* ((buffer-read-only)
11137          (gnus-summary-prepare-hook nil))
11138     ;; We do the sorting by regenerating the threads.
11139     (gnus-summary-prepare)
11140     ;; Hide subthreads if needed.
11141     (gnus-summary-maybe-hide-threads)))
11142
11143 (defun gnus-summary-sort (predicate reverse)
11144   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11145   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11146          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11147          (gnus-thread-sort-functions
11148           (if (not reverse)
11149               thread
11150             `(lambda (t1 t2)
11151                (,thread t2 t1))))
11152          (gnus-sort-gathered-threads-function
11153           gnus-thread-sort-functions)
11154          (gnus-article-sort-functions
11155           (if (not reverse)
11156               article
11157             `(lambda (t1 t2)
11158                (,article t2 t1))))
11159          (buffer-read-only)
11160          (gnus-summary-prepare-hook nil))
11161     ;; We do the sorting by regenerating the threads.
11162     (gnus-summary-prepare)
11163     ;; Hide subthreads if needed.
11164     (gnus-summary-maybe-hide-threads)))
11165
11166 ;; Summary saving commands.
11167
11168 (defun gnus-summary-save-article (&optional n not-saved)
11169   "Save the current article using the default saver function.
11170 If N is a positive number, save the N next articles.
11171 If N is a negative number, save the N previous articles.
11172 If N is nil and any articles have been marked with the process mark,
11173 save those articles instead.
11174 The variable `gnus-default-article-saver' specifies the saver function."
11175   (interactive "P")
11176   (let* ((articles (gnus-summary-work-articles n))
11177          (save-buffer (save-excursion
11178                         (nnheader-set-temp-buffer " *Gnus Save*")))
11179          (num (length articles))
11180          header file)
11181     (dolist (article articles)
11182       (setq header (gnus-summary-article-header article))
11183       (if (not (vectorp header))
11184           ;; This is a pseudo-article.
11185           (if (assq 'name header)
11186               (gnus-copy-file (cdr (assq 'name header)))
11187             (gnus-message 1 "Article %d is unsaveable" article))
11188         ;; This is a real article.
11189         (save-window-excursion
11190           (let ((gnus-display-mime-function nil)
11191                 (gnus-article-prepare-hook nil))
11192             (gnus-summary-select-article t nil nil article)))
11193         (save-excursion
11194           (set-buffer save-buffer)
11195           (erase-buffer)
11196           (insert-buffer-substring gnus-original-article-buffer))
11197         (setq file (gnus-article-save save-buffer file num))
11198         (gnus-summary-remove-process-mark article)
11199         (unless not-saved
11200           (gnus-summary-set-saved-mark article))))
11201     (gnus-kill-buffer save-buffer)
11202     (gnus-summary-position-point)
11203     (gnus-set-mode-line 'summary)
11204     n))
11205
11206 (defun gnus-summary-pipe-output (&optional arg headers)
11207   "Pipe the current article to a subprocess.
11208 If N is a positive number, pipe the N next articles.
11209 If N is a negative number, pipe the N previous articles.
11210 If N is nil and any articles have been marked with the process mark,
11211 pipe those articles instead.
11212 If HEADERS (the symbolic prefix), include the headers, too."
11213   (interactive (gnus-interactive "P\ny"))
11214   (require 'gnus-art)
11215   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
11216         (gnus-save-all-headers (or headers gnus-save-all-headers)))
11217     (gnus-summary-save-article arg t))
11218   (let ((buffer (get-buffer "*Shell Command Output*")))
11219     (when (and buffer
11220                (not (zerop (buffer-size buffer))))
11221       (gnus-configure-windows 'pipe))))
11222
11223 (defun gnus-summary-save-article-mail (&optional arg)
11224   "Append the current article to an mail file.
11225 If N is a positive number, save the N next articles.
11226 If N is a negative number, save the N previous articles.
11227 If N is nil and any articles have been marked with the process mark,
11228 save those articles instead."
11229   (interactive "P")
11230   (require 'gnus-art)
11231   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11232     (gnus-summary-save-article arg)))
11233
11234 (defun gnus-summary-save-article-rmail (&optional arg)
11235   "Append the current article to an rmail file.
11236 If N is a positive number, save the N next articles.
11237 If N is a negative number, save the N previous articles.
11238 If N is nil and any articles have been marked with the process mark,
11239 save those articles instead."
11240   (interactive "P")
11241   (require 'gnus-art)
11242   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11243     (gnus-summary-save-article arg)))
11244
11245 (defun gnus-summary-save-article-file (&optional arg)
11246   "Append the current article to a file.
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   (interactive "P")
11252   (require 'gnus-art)
11253   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11254     (gnus-summary-save-article arg)))
11255
11256 (defun gnus-summary-write-article-file (&optional arg)
11257   "Write the current article to a file, deleting the previous file.
11258 If N is a positive number, save the N next articles.
11259 If N is a negative number, save the N previous articles.
11260 If N is nil and any articles have been marked with the process mark,
11261 save those articles instead."
11262   (interactive "P")
11263   (require 'gnus-art)
11264   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11265     (gnus-summary-save-article arg)))
11266
11267 (defun gnus-summary-save-article-body-file (&optional arg)
11268   "Append the current article body to a file.
11269 If N is a positive number, save the N next articles.
11270 If N is a negative number, save the N previous articles.
11271 If N is nil and any articles have been marked with the process mark,
11272 save those articles instead."
11273   (interactive "P")
11274   (require 'gnus-art)
11275   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11276     (gnus-summary-save-article arg)))
11277
11278 (defun gnus-summary-muttprint (&optional arg)
11279   "Print the current article using Muttprint.
11280 If N is a positive number, save the N next articles.
11281 If N is a negative number, save the N previous articles.
11282 If N is nil and any articles have been marked with the process mark,
11283 save those articles instead."
11284   (interactive "P")
11285   (require 'gnus-art)
11286   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11287     (gnus-summary-save-article arg t)))
11288
11289 (defun gnus-summary-pipe-message (program)
11290   "Pipe the current article through PROGRAM."
11291   (interactive "sProgram: ")
11292   (gnus-summary-select-article)
11293   (let ((mail-header-separator ""))
11294     (gnus-eval-in-buffer-window gnus-article-buffer
11295       (save-restriction
11296         (widen)
11297         (let ((start (window-start))
11298               buffer-read-only)
11299           (message-pipe-buffer-body program)
11300           (set-window-start (get-buffer-window (current-buffer)) start))))))
11301
11302 (defun gnus-get-split-value (methods)
11303   "Return a value based on the split METHODS."
11304   (let (split-name method result match)
11305     (when methods
11306       (save-excursion
11307         (set-buffer gnus-original-article-buffer)
11308         (save-restriction
11309           (nnheader-narrow-to-headers)
11310           (while (and methods (not split-name))
11311             (goto-char (point-min))
11312             (setq method (pop methods))
11313             (setq match (car method))
11314             (when (cond
11315                    ((stringp match)
11316                     ;; Regular expression.
11317                     (ignore-errors
11318                       (re-search-forward match nil t)))
11319                    ((functionp match)
11320                     ;; Function.
11321                     (save-restriction
11322                       (widen)
11323                       (setq result (funcall match gnus-newsgroup-name))))
11324                    ((consp match)
11325                     ;; Form.
11326                     (save-restriction
11327                       (widen)
11328                       (setq result (eval match)))))
11329               (setq split-name (cdr method))
11330               (cond ((stringp result)
11331                      (push (expand-file-name
11332                             result gnus-article-save-directory)
11333                            split-name))
11334                     ((consp result)
11335                      (setq split-name (append result split-name)))))))))
11336     (nreverse split-name)))
11337
11338 (defun gnus-valid-move-group-p (group)
11339   (and (boundp group)
11340        (symbol-name group)
11341        (symbol-value group)
11342        (gnus-get-function (gnus-find-method-for-group
11343                            (symbol-name group)) 'request-accept-article t)))
11344
11345 (defun gnus-read-move-group-name (prompt default articles prefix)
11346   "Read a group name."
11347   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11348          (minibuffer-confirm-incomplete nil) ; XEmacs
11349          (prom
11350           (format "%s %s to"
11351                   prompt
11352                   (if (> (length articles) 1)
11353                       (format "these %d articles" (length articles))
11354                     "this article")))
11355          (to-newsgroup
11356           (cond
11357            ((null split-name)
11358             (gnus-completing-read-with-default
11359              default prom
11360              gnus-active-hashtb
11361              'gnus-valid-move-group-p
11362              nil prefix
11363              'gnus-group-history))
11364            ((= 1 (length split-name))
11365             (gnus-completing-read-with-default
11366              (car split-name) prom
11367              gnus-active-hashtb
11368              'gnus-valid-move-group-p
11369              nil nil
11370              'gnus-group-history))
11371            (t
11372             (gnus-completing-read-with-default
11373              nil prom
11374              (mapcar 'list (nreverse split-name))
11375              nil nil nil
11376              'gnus-group-history))))
11377          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11378     (when to-newsgroup
11379       (if (or (string= to-newsgroup "")
11380               (string= to-newsgroup prefix))
11381           (setq to-newsgroup default))
11382       (unless to-newsgroup
11383         (error "No group name entered"))
11384       (or (gnus-active to-newsgroup)
11385           (gnus-activate-group to-newsgroup nil nil to-method)
11386           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11387                                      to-newsgroup))
11388               (or (and (gnus-request-create-group to-newsgroup to-method)
11389                        (gnus-activate-group
11390                         to-newsgroup nil nil to-method)
11391                        (gnus-subscribe-group to-newsgroup))
11392                   (error "Couldn't create group %s" to-newsgroup)))
11393           (error "No such group: %s" to-newsgroup)))
11394     to-newsgroup))
11395
11396 (defun gnus-summary-save-parts (type dir n &optional reverse)
11397   "Save parts matching TYPE to DIR.
11398 If REVERSE, save parts that do not match TYPE."
11399   (interactive
11400    (list (read-string "Save parts of type: "
11401                       (or (car gnus-summary-save-parts-type-history)
11402                           gnus-summary-save-parts-default-mime)
11403                       'gnus-summary-save-parts-type-history)
11404          (setq gnus-summary-save-parts-last-directory
11405                (read-file-name "Save to directory: "
11406                                gnus-summary-save-parts-last-directory
11407                                nil t))
11408          current-prefix-arg))
11409   (gnus-summary-iterate n
11410     (let ((gnus-display-mime-function nil)
11411           (gnus-inhibit-treatment t))
11412       (gnus-summary-select-article))
11413     (save-excursion
11414       (set-buffer gnus-article-buffer)
11415       (let ((handles (or gnus-article-mime-handles
11416                          (mm-dissect-buffer nil gnus-article-loose-mime)
11417                          (and gnus-article-emulate-mime
11418                               (mm-uu-dissect)))))
11419         (when handles
11420           (gnus-summary-save-parts-1 type dir handles reverse)
11421           (unless gnus-article-mime-handles ;; Don't destroy this case.
11422             (mm-destroy-parts handles)))))))
11423
11424 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11425   (if (stringp (car handle))
11426       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11427               (cdr handle))
11428     (when (if reverse
11429               (not (string-match type (mm-handle-media-type handle)))
11430             (string-match type (mm-handle-media-type handle)))
11431       (let ((file (expand-file-name
11432                    (gnus-map-function
11433                     mm-file-name-rewrite-functions
11434                     (file-name-nondirectory
11435                      (or
11436                       (mail-content-type-get
11437                        (mm-handle-disposition handle) 'filename)
11438                       (mail-content-type-get
11439                        (mm-handle-type handle) 'name)
11440                       (concat gnus-newsgroup-name
11441                               "." (number-to-string
11442                                    (cdr gnus-article-current))))))
11443                    dir)))
11444         (unless (file-exists-p file)
11445           (mm-save-part-to-file handle file))))))
11446
11447 ;; Summary extract commands
11448
11449 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11450   (let ((buffer-read-only nil)
11451         (article (gnus-summary-article-number))
11452         after-article b e)
11453     (unless (gnus-summary-goto-subject article)
11454       (error "No such article: %d" article))
11455     (gnus-summary-position-point)
11456     ;; If all commands are to be bunched up on one line, we collect
11457     ;; them here.
11458     (unless gnus-view-pseudos-separately
11459       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11460             files action)
11461         (while ps
11462           (setq action (cdr (assq 'action (car ps))))
11463           (setq files (list (cdr (assq 'name (car ps)))))
11464           (while (and ps (cdr ps)
11465                       (string= (or action "1")
11466                                (or (cdr (assq 'action (cadr ps))) "2")))
11467             (push (cdr (assq 'name (cadr ps))) files)
11468             (setcdr ps (cddr ps)))
11469           (when files
11470             (when (not (string-match "%s" action))
11471               (push " " files))
11472             (push " " files)
11473             (when (assq 'execute (car ps))
11474               (setcdr (assq 'execute (car ps))
11475                       (funcall (if (string-match "%s" action)
11476                                    'format 'concat)
11477                                action
11478                                (mapconcat
11479                                 (lambda (f)
11480                                   (if (equal f " ")
11481                                       f
11482                                     (shell-quote-argument f)))
11483                                 files " ")))))
11484           (setq ps (cdr ps)))))
11485     (if (and gnus-view-pseudos (not not-view))
11486         (while pslist
11487           (when (assq 'execute (car pslist))
11488             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11489                                   (eq gnus-view-pseudos 'not-confirm)))
11490           (setq pslist (cdr pslist)))
11491       (save-excursion
11492         (while pslist
11493           (setq after-article (or (cdr (assq 'article (car pslist)))
11494                                   (gnus-summary-article-number)))
11495           (gnus-summary-goto-subject after-article)
11496           (forward-line 1)
11497           (setq b (point))
11498           (insert "    " (file-name-nondirectory
11499                           (cdr (assq 'name (car pslist))))
11500                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11501           (setq e (point))
11502           (forward-line -1)             ; back to `b'
11503           (gnus-add-text-properties
11504            b (1- e) (list 'gnus-number gnus-reffed-article-number
11505                           gnus-mouse-face-prop gnus-mouse-face))
11506           (gnus-data-enter
11507            after-article gnus-reffed-article-number
11508            gnus-unread-mark b (car pslist) 0 (- e b))
11509           (setq gnus-newsgroup-unreads
11510                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11511                                          gnus-reffed-article-number))
11512           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11513           (setq pslist (cdr pslist)))))))
11514
11515 (defun gnus-pseudos< (p1 p2)
11516   (let ((c1 (cdr (assq 'action p1)))
11517         (c2 (cdr (assq 'action p2))))
11518     (and c1 c2 (string< c1 c2))))
11519
11520 (defun gnus-request-pseudo-article (props)
11521   (cond ((assq 'execute props)
11522          (gnus-execute-command (cdr (assq 'execute props)))))
11523   (let ((gnus-current-article (gnus-summary-article-number)))
11524     (gnus-run-hooks 'gnus-mark-article-hook)))
11525
11526 (defun gnus-execute-command (command &optional automatic)
11527   (save-excursion
11528     (gnus-article-setup-buffer)
11529     (set-buffer gnus-article-buffer)
11530     (setq buffer-read-only nil)
11531     (let ((command (if automatic command
11532                      (read-string "Command: " (cons command 0)))))
11533       (erase-buffer)
11534       (insert "$ " command "\n\n")
11535       (if gnus-view-pseudo-asynchronously
11536           (start-process "gnus-execute" (current-buffer) shell-file-name
11537                          shell-command-switch command)
11538         (call-process shell-file-name nil t nil
11539                       shell-command-switch command)))))
11540
11541 ;; Summary kill commands.
11542
11543 (defun gnus-summary-edit-global-kill (article)
11544   "Edit the \"global\" kill file."
11545   (interactive (list (gnus-summary-article-number)))
11546   (gnus-group-edit-global-kill article))
11547
11548 (defun gnus-summary-edit-local-kill ()
11549   "Edit a local kill file applied to the current newsgroup."
11550   (interactive)
11551   (setq gnus-current-headers (gnus-summary-article-header))
11552   (gnus-group-edit-local-kill
11553    (gnus-summary-article-number) gnus-newsgroup-name))
11554
11555 ;;; Header reading.
11556
11557 (defun gnus-read-header (id &optional header)
11558   "Read the headers of article ID and enter them into the Gnus system."
11559   (let ((group gnus-newsgroup-name)
11560         (gnus-override-method
11561          (or
11562           gnus-override-method
11563           (and (gnus-news-group-p gnus-newsgroup-name)
11564                (car (gnus-refer-article-methods)))))
11565         where)
11566     ;; First we check to see whether the header in question is already
11567     ;; fetched.
11568     (if (stringp id)
11569         ;; This is a Message-ID.
11570         (setq header (or header (gnus-id-to-header id)))
11571       ;; This is an article number.
11572       (setq header (or header (gnus-summary-article-header id))))
11573     (if (and header
11574              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11575         ;; We have found the header.
11576         header
11577       ;; We have to really fetch the header to this article.
11578       (save-excursion
11579         (set-buffer nntp-server-buffer)
11580         (when (setq where (gnus-request-head id group))
11581           (nnheader-fold-continuation-lines)
11582           (goto-char (point-max))
11583           (insert ".\n")
11584           (goto-char (point-min))
11585           (insert "211 ")
11586           (princ (cond
11587                   ((numberp id) id)
11588                   ((cdr where) (cdr where))
11589                   (header (mail-header-number header))
11590                   (t gnus-reffed-article-number))
11591                  (current-buffer))
11592           (insert " Article retrieved.\n"))
11593         (if (or (not where)
11594                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11595             ()                          ; Malformed head.
11596           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11597             (when (and (stringp id)
11598                        (not (string= (gnus-group-real-name group)
11599                                      (car where))))
11600               ;; If we fetched by Message-ID and the article came
11601               ;; from a different group, we fudge some bogus article
11602               ;; numbers for this article.
11603               (mail-header-set-number header gnus-reffed-article-number))
11604             (save-excursion
11605               (set-buffer gnus-summary-buffer)
11606               (decf gnus-reffed-article-number)
11607               (gnus-remove-header (mail-header-number header))
11608               (push header gnus-newsgroup-headers)
11609               (setq gnus-current-headers header)
11610               (push (mail-header-number header) gnus-newsgroup-limit)))
11611           header)))))
11612
11613 (defun gnus-remove-header (number)
11614   "Remove header NUMBER from `gnus-newsgroup-headers'."
11615   (if (and gnus-newsgroup-headers
11616            (= number (mail-header-number (car gnus-newsgroup-headers))))
11617       (pop gnus-newsgroup-headers)
11618     (let ((headers gnus-newsgroup-headers))
11619       (while (and (cdr headers)
11620                   (not (= number (mail-header-number (cadr headers)))))
11621         (pop headers))
11622       (when (cdr headers)
11623         (setcdr headers (cddr headers))))))
11624
11625 ;;;
11626 ;;; summary highlights
11627 ;;;
11628
11629 (defun gnus-highlight-selected-summary ()
11630   "Highlight selected article in summary buffer."
11631   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11632   (when gnus-summary-selected-face
11633     (save-excursion
11634       (let* ((beg (point-at-bol))
11635              (end (point-at-eol))
11636              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11637              (from (if (get-text-property beg gnus-mouse-face-prop)
11638                        beg
11639                      (or (next-single-property-change
11640                           beg gnus-mouse-face-prop nil end)
11641                          beg)))
11642              (to
11643               (if (= from end)
11644                   (- from 2)
11645                 (or (next-single-property-change
11646                      from gnus-mouse-face-prop nil end)
11647                     end))))
11648         ;; If no mouse-face prop on line we will have to = from = end,
11649         ;; so we highlight the entire line instead.
11650         (when (= (+ to 2) from)
11651           (setq from beg)
11652           (setq to end))
11653         (if gnus-newsgroup-selected-overlay
11654             ;; Move old overlay.
11655             (gnus-move-overlay
11656              gnus-newsgroup-selected-overlay from to (current-buffer))
11657           ;; Create new overlay.
11658           (gnus-overlay-put
11659            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11660            'face gnus-summary-selected-face))))))
11661
11662 (defvar gnus-summary-highlight-line-cached nil)
11663 (defvar gnus-summary-highlight-line-trigger nil)
11664
11665 (defun gnus-summary-highlight-line-0 ()
11666   (if (and (eq gnus-summary-highlight-line-trigger
11667                gnus-summary-highlight)
11668            gnus-summary-highlight-line-cached)
11669       gnus-summary-highlight-line-cached
11670     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11671           gnus-summary-highlight-line-cached
11672           (let* ((cond (list 'cond))
11673                  (c cond)
11674                  (list gnus-summary-highlight))
11675             (while list
11676               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11677                               nil))
11678               (setq c (cdr c)
11679                     list (cdr list)))
11680             (gnus-byte-compile (list 'lambda nil cond))))))
11681
11682 (defun gnus-summary-highlight-line ()
11683   "Highlight current line according to `gnus-summary-highlight'."
11684   (let* ((beg (point-at-bol))
11685          (article (or (gnus-summary-article-number) gnus-current-article))
11686          (score (or (cdr (assq article
11687                                gnus-newsgroup-scored))
11688                     gnus-summary-default-score 0))
11689          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11690          (inhibit-read-only t)
11691          (default gnus-summary-default-score)
11692          (default-high gnus-summary-default-high-score)
11693          (default-low gnus-summary-default-low-score)
11694          (uncached (and gnus-summary-use-undownloaded-faces
11695                         (memq article gnus-newsgroup-undownloaded)
11696                         (not (memq article gnus-newsgroup-cached)))))
11697     (let ((face (funcall (gnus-summary-highlight-line-0))))
11698       (unless (eq face (get-text-property beg 'face))
11699         (gnus-put-text-property-excluding-characters-with-faces
11700          beg (point-at-eol) 'face
11701          (setq face (if (boundp face) (symbol-value face) face)))
11702         (when gnus-summary-highlight-line-function
11703           (funcall gnus-summary-highlight-line-function article face))))))
11704
11705 (defun gnus-update-read-articles (group unread &optional compute)
11706   "Update the list of read articles in GROUP.
11707 UNREAD is a sorted list."
11708   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11709         (info (gnus-get-info group))
11710         (prev 1)
11711         read)
11712     (if (or (not info) (not active))
11713         ;; There is no info on this group if it was, in fact,
11714         ;; killed.  Gnus stores no information on killed groups, so
11715         ;; there's nothing to be done.
11716         ;; One could store the information somewhere temporarily,
11717         ;; perhaps...  Hmmm...
11718         ()
11719       ;; Remove any negative articles numbers.
11720       (while (and unread (< (car unread) 0))
11721         (setq unread (cdr unread)))
11722       ;; Remove any expired article numbers
11723       (while (and unread (< (car unread) (car active)))
11724         (setq unread (cdr unread)))
11725       ;; Compute the ranges of read articles by looking at the list of
11726       ;; unread articles.
11727       (while unread
11728         (when (/= (car unread) prev)
11729           (push (if (= prev (1- (car unread))) prev
11730                   (cons prev (1- (car unread))))
11731                 read))
11732         (setq prev (1+ (car unread)))
11733         (setq unread (cdr unread)))
11734       (when (<= prev (cdr active))
11735         (push (cons prev (cdr active)) read))
11736       (setq read (if (> (length read) 1) (nreverse read) read))
11737       (if compute
11738           read
11739         (save-excursion
11740           (let (setmarkundo)
11741             ;; Propagate the read marks to the backend.
11742             (when (gnus-check-backend-function 'request-set-mark group)
11743               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11744                     (add (gnus-remove-from-range read (gnus-info-read info))))
11745                 (when (or add del)
11746                   (unless (gnus-check-group group)
11747                     (error "Can't open server for %s" group))
11748                   (gnus-request-set-mark
11749                    group (delq nil (list (if add (list add 'add '(read)))
11750                                          (if del (list del 'del '(read))))))
11751                   (setq setmarkundo
11752                         `(gnus-request-set-mark
11753                           ,group
11754                           ',(delq nil (list
11755                                        (if del (list del 'add '(read)))
11756                                        (if add (list add 'del '(read))))))))))
11757             (set-buffer gnus-group-buffer)
11758             (gnus-undo-register
11759               `(progn
11760                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11761                  (gnus-info-set-read ',info ',(gnus-info-read info))
11762                  (gnus-get-unread-articles-in-group ',info
11763                                                     (gnus-active ,group))
11764                  (gnus-group-update-group ,group t)
11765                  ,setmarkundo))))
11766         ;; Enter this list into the group info.
11767         (gnus-info-set-read info read)
11768         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11769         (gnus-get-unread-articles-in-group info (gnus-active group))
11770         t))))
11771
11772 (defun gnus-offer-save-summaries ()
11773   "Offer to save all active summary buffers."
11774   (let (buffers)
11775     ;; Go through all buffers and find all summaries.
11776     (dolist (buffer (buffer-list))
11777       (when (and (setq buffer (buffer-name buffer))
11778                  (string-match "Summary" buffer)
11779                  (with-current-buffer buffer
11780                    ;; We check that this is, indeed, a summary buffer.
11781                    (and (eq major-mode 'gnus-summary-mode)
11782                         ;; Also make sure this isn't bogus.
11783                         gnus-newsgroup-prepared
11784                         ;; Also make sure that this isn't a
11785                         ;; dead summary buffer.
11786                         (not gnus-dead-summary-mode))))
11787         (push buffer buffers)))
11788     ;; Go through all these summary buffers and offer to save them.
11789     (when buffers
11790       (save-excursion
11791         (map-y-or-n-p
11792          "Update summary buffer %s? "
11793          (lambda (buf)
11794            (switch-to-buffer buf)
11795            (gnus-summary-exit))
11796          buffers)))))
11797
11798 (defun gnus-summary-setup-default-charset ()
11799   "Setup newsgroup default charset."
11800   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11801       (setq gnus-newsgroup-charset nil)
11802     (let* ((ignored-charsets
11803             (or gnus-newsgroup-ephemeral-ignored-charsets
11804                 (append
11805                  (and gnus-newsgroup-name
11806                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11807                  gnus-newsgroup-ignored-charsets))))
11808       (setq gnus-newsgroup-charset
11809             (or gnus-newsgroup-ephemeral-charset
11810                 (and gnus-newsgroup-name
11811                      (gnus-parameter-charset gnus-newsgroup-name))
11812                 gnus-default-charset))
11813       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11814            ignored-charsets))))
11815
11816 ;;;
11817 ;;; Mime Commands
11818 ;;;
11819
11820 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11821   "Display the current article buffer fully MIME-buttonized.
11822 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11823 treated as multipart/mixed."
11824   (interactive "P")
11825   (require 'gnus-art)
11826   (let ((gnus-unbuttonized-mime-types nil)
11827         (gnus-mime-display-multipart-as-mixed show-all-parts))
11828     (gnus-summary-show-article)))
11829
11830 (defun gnus-summary-repair-multipart (article)
11831   "Add a Content-Type header to a multipart article without one."
11832   (interactive (list (gnus-summary-article-number)))
11833   (gnus-with-article article
11834     (message-narrow-to-head)
11835     (message-remove-header "Mime-Version")
11836     (goto-char (point-max))
11837     (insert "Mime-Version: 1.0\n")
11838     (widen)
11839     (when (search-forward "\n--" nil t)
11840       (let ((separator (buffer-substring (point) (point-at-eol))))
11841         (message-narrow-to-head)
11842         (message-remove-header "Content-Type")
11843         (goto-char (point-max))
11844         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11845                         separator))
11846         (widen))))
11847   (let (gnus-mark-article-hook)
11848     (gnus-summary-select-article t t nil article)))
11849
11850 (defun gnus-summary-toggle-display-buttonized ()
11851   "Toggle the buttonizing of the article buffer."
11852   (interactive)
11853   (require 'gnus-art)
11854   (if (setq gnus-inhibit-mime-unbuttonizing
11855             (not gnus-inhibit-mime-unbuttonizing))
11856       (let ((gnus-unbuttonized-mime-types nil))
11857         (gnus-summary-show-article))
11858     (gnus-summary-show-article)))
11859
11860 ;;;
11861 ;;; Generic summary marking commands
11862 ;;;
11863
11864 (defvar gnus-summary-marking-alist
11865   '((read gnus-del-mark "d")
11866     (unread gnus-unread-mark "u")
11867     (ticked gnus-ticked-mark "!")
11868     (dormant gnus-dormant-mark "?")
11869     (expirable gnus-expirable-mark "e"))
11870   "An alist of names/marks/keystrokes.")
11871
11872 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11873 (defvar gnus-summary-mark-map)
11874
11875 (defun gnus-summary-make-all-marking-commands ()
11876   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11877   (dolist (elem gnus-summary-marking-alist)
11878     (apply 'gnus-summary-make-marking-command elem)))
11879
11880 (defun gnus-summary-make-marking-command (name mark keystroke)
11881   (let ((map (make-sparse-keymap)))
11882     (define-key gnus-summary-generic-mark-map keystroke map)
11883     (dolist (lway `((next "next" next nil "n")
11884                     (next-unread "next unread" next t "N")
11885                     (prev "previous" prev nil "p")
11886                     (prev-unread "previous unread" prev t "P")
11887                     (nomove "" nil nil ,keystroke)))
11888       (let ((func (gnus-summary-make-marking-command-1
11889                    mark (car lway) lway name)))
11890         (setq func (eval func))
11891         (define-key map (nth 4 lway) func)))))
11892
11893 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11894   `(defun ,(intern
11895             (format "gnus-summary-put-mark-as-%s%s"
11896                     name (if (eq way 'nomove)
11897                              ""
11898                            (concat "-" (symbol-name way)))))
11899      (n)
11900      ,(format
11901        "Mark the current article as %s%s.
11902 If N, the prefix, then repeat N times.
11903 If N is negative, move in reverse order.
11904 The difference between N and the actual number of articles marked is
11905 returned."
11906        name (cadr lway))
11907      (interactive "p")
11908      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11909
11910 (defun gnus-summary-generic-mark (n mark move unread)
11911   "Mark N articles with MARK."
11912   (unless (eq major-mode 'gnus-summary-mode)
11913     (error "This command can only be used in the summary buffer"))
11914   (gnus-summary-show-thread)
11915   (let ((nummove
11916          (cond
11917           ((eq move 'next) 1)
11918           ((eq move 'prev) -1)
11919           (t 0))))
11920     (if (zerop nummove)
11921         (setq n 1)
11922       (when (< n 0)
11923         (setq n (abs n)
11924               nummove (* -1 nummove))))
11925     (while (and (> n 0)
11926                 (gnus-summary-mark-article nil mark)
11927                 (zerop (gnus-summary-next-subject nummove unread t)))
11928       (setq n (1- n)))
11929     (when (/= 0 n)
11930       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11931     (gnus-summary-recenter)
11932     (gnus-summary-position-point)
11933     (gnus-set-mode-line 'summary)
11934     n))
11935
11936 (defun gnus-summary-insert-articles (articles)
11937   (when (setq articles
11938               (gnus-sorted-difference articles
11939                                       (mapcar (lambda (h)
11940                                                 (mail-header-number h))
11941                                               gnus-newsgroup-headers)))
11942     (setq gnus-newsgroup-headers
11943           (gnus-merge 'list
11944                       gnus-newsgroup-headers
11945                       (gnus-fetch-headers articles)
11946                       'gnus-article-sort-by-number))
11947     ;; Suppress duplicates?
11948     (when gnus-suppress-duplicates
11949       (gnus-dup-suppress-articles))
11950
11951     ;; We might want to build some more threads first.
11952     (when (and gnus-fetch-old-headers
11953                (eq gnus-headers-retrieved-by 'nov))
11954       (if (eq gnus-fetch-old-headers 'invisible)
11955           (gnus-build-all-threads)
11956         (gnus-build-old-threads)))
11957     ;; Let the Gnus agent mark articles as read.
11958     (when gnus-agent
11959       (gnus-agent-get-undownloaded-list))
11960     ;; Remove list identifiers from subject
11961     (when gnus-list-identifiers
11962       (gnus-summary-remove-list-identifiers))
11963     ;; First and last article in this newsgroup.
11964     (when gnus-newsgroup-headers
11965       (setq gnus-newsgroup-begin
11966             (mail-header-number (car gnus-newsgroup-headers))
11967             gnus-newsgroup-end
11968             (mail-header-number
11969              (gnus-last-element gnus-newsgroup-headers))))
11970     (when gnus-use-scoring
11971       (gnus-possibly-score-headers))))
11972
11973 (defun gnus-summary-insert-old-articles (&optional all)
11974   "Insert all old articles in this group.
11975 If ALL is non-nil, already read articles become readable.
11976 If ALL is a number, fetch this number of articles."
11977   (interactive "P")
11978   (prog1
11979       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11980             older len)
11981         (setq older
11982               ;; Some nntp servers lie about their active range.  When
11983               ;; this happens, the active range can be in the millions.
11984               ;; Use a compressed range to avoid creating a huge list.
11985               (gnus-range-difference (list gnus-newsgroup-active) old))
11986         (setq len (gnus-range-length older))
11987         (cond
11988          ((null older) nil)
11989          ((numberp all)
11990           (if (< all len)
11991               (let ((older-range (nreverse older)))
11992                 (setq older nil)
11993
11994                 (while (> all 0)
11995                   (let* ((r (pop older-range))
11996                          (min (if (numberp r) r (car r)))
11997                          (max (if (numberp r) r (cdr r))))
11998                     (while (and (<= min max)
11999                                 (> all 0))
12000                       (push max older)
12001                       (setq all (1- all)
12002                             max (1- max))))))
12003             (setq older (gnus-uncompress-range older))))
12004          (all
12005           (setq older (gnus-uncompress-range older)))
12006          (t
12007           (when (and (numberp gnus-large-newsgroup)
12008                    (> len gnus-large-newsgroup))
12009               (let* ((cursor-in-echo-area nil)
12010                      (initial (gnus-parameter-large-newsgroup-initial
12011                                gnus-newsgroup-name))
12012                      (input
12013                       (read-string
12014                        (format
12015                         "How many articles from %s (%s %d): "
12016                         (gnus-limit-string
12017                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
12018                         (if initial "max" "default")
12019                         len)
12020                        (if initial
12021                            (cons (number-to-string initial)
12022                                  0)))))
12023                 (unless (string-match "^[ \t]*$" input)
12024                   (setq all (string-to-number input))
12025                   (if (< all len)
12026                       (let ((older-range (nreverse older)))
12027                         (setq older nil)
12028
12029                         (while (> all 0)
12030                           (let* ((r (pop older-range))
12031                                  (min (if (numberp r) r (car r)))
12032                                  (max (if (numberp r) r (cdr r))))
12033                             (while (and (<= min max)
12034                                         (> all 0))
12035                               (push max older)
12036                               (setq all (1- all)
12037                                     max (1- max))))))))))
12038           (setq older (gnus-uncompress-range older))))
12039         (if (not older)
12040             (message "No old news.")
12041           (gnus-summary-insert-articles older)
12042           (gnus-summary-limit (gnus-sorted-nunion old older))))
12043     (gnus-summary-position-point)))
12044
12045 (defun gnus-summary-insert-new-articles ()
12046   "Insert all new articles in this group."
12047   (interactive)
12048   (prog1
12049       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12050             (old-active gnus-newsgroup-active)
12051             (nnmail-fetched-sources (list t))
12052             i new)
12053         (setq gnus-newsgroup-active
12054               (gnus-activate-group gnus-newsgroup-name 'scan))
12055         (setq i (cdr gnus-newsgroup-active))
12056         (while (> i (cdr old-active))
12057           (push i new)
12058           (decf i))
12059         (if (not new)
12060             (message "No gnus is bad news")
12061           (gnus-summary-insert-articles new)
12062           (setq gnus-newsgroup-unreads
12063                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12064           (gnus-summary-limit (gnus-sorted-nunion old new))))
12065     (gnus-summary-position-point)))
12066
12067 (gnus-summary-make-all-marking-commands)
12068
12069 (gnus-ems-redefine)
12070
12071 (provide 'gnus-sum)
12072
12073 (run-hooks 'gnus-sum-load-hook)
12074
12075 ;; Local Variables:
12076 ;; coding: iso-8859-1
12077 ;; End:
12078
12079 ;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
12080 ;;; gnus-sum.el ends here